Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Thanks Sedj, but could you look at this? 1

Status
Not open for further replies.

brandonstone

Technical User
Sep 15, 2002
2
0
0
US
This is the code I've written so far, and I'm not sure where to include the int intOne = Integer.parseInt(args[1]) in order to acheive the desired results. I hope this isn't a stupid question. Should the code you suggested go in the main method or in the readTokens()? Thanks for any help you might be able to provide.


import cs1.Keyboard;
import java.util.*;

public class Search
{
public int maxArrayCalls, maxArray2Calls;

public static void main(String[] args)
{
String NumList;

System.out.println ("Enter some numbers (at least one) and this program will " +
"determine which number is the largest.");
System.out.print ("Enter numbers here: ");
NumList = Keyboard.readString();

// Test for NumList
System.out.println ("Test: " + NumList);

String[] NumArray = readTokens(NumList, " ");

// Test for NumArray
for (int i = 0; i <= NumArray.length; i++)
{
System.out.println (NumArray);
if (NumArray[0] > NumArray[1])
{
System.out.println (NumArray[0]);
}else
System.out.println (NumArray[1]);
}

}//end main

public static String[] readTokens(String text, String token)
{
StringTokenizer parser = new StringTokenizer(text, token);
int numTokens = parser.countTokens();
String[] list = new int[numTokens];

for (int i = 0; i < numTokens; i++)
{
list = (int)parser.nextToken();
}// end for-loop

return list;
}//end readTokens

}// end recursiveSearch
 
Your problem is coming from the fact that Search.readTokens is incorrect. You define the method as returning a String array, which doesn't seem like what you want, yet in the method you define a String array and initialize it as an int array. This is obviously never going to compile.

I think what you want is for Search.readTokens to return an int[] and the declaration of list to be int[].

Your next problem is this line:
Code:
 list = (int)parser.nextToken();
This is incorrect. You cannot directly cast a String into an int. It is here that you must use Integer.parseInt to convert your String value into an int value.

Finally, there is a problem with directly accessing NumArray[1]... what if the user enters only one number? You program is going to blowup with an ArrayOutOfBoundsException. This problem I leave up to you to solve.

BTW, I suggest you read Sun's document on Java Naming Conventions at
 
Just to add ...
In order to negate the ArrayOutOfBoundsException problem, I would suggest using java.util.Vector - which are in effect linked lists, and so can grow/shrink without causing the above error
 
Negative Sedj

Please get your story straight before posting--misinformation is rampant on these forums and I don't think that you want to contribute to it. There is a fundamental difference between array based lists and linked lists. Their runtime behaviour and efficiency is drastically difference (for example, a get(int) operation in an arraylist is O(1) however in a linked list it is O(n)). I'd suggest that you pick up a good data structures book if you do not know this.

Vector is NOT a linked list. It uses a resizeable Object array as it's backing data structure so is more closely an ArrayList. In fact, the primary difference between ArrayList and Vector is that most of Vectors methods are synchronized. For this reason, most people now use ArrayList instead of Vector unless you require the synchronization. For that matter, you can use the Collections.synchronizedList(...) to get one anyways.

</soapbox>
 
meadandale,

I agree with you, there is a lot of misinformation around, and I am by no means adverse to critcism - we all learn from constructive corrections. However my point about how Vectors are in effect linked lists is this (please correct me if infact incorrect [and if am again will gladly go back to the bookshop!]) :
When the Vector (nee Object array) reserves a space in memory, it must have a pointer to this memory, and in order to request more memory for another element in the Vector, there must be a reference to the former elements data address (because at the time the first element in the Vector was allocated, there was no pre-knowledge of how big this object was going to get, or how many elements were in it) ... hence similar to the linked list in C ??
Or not ?!!!
 
Nope, that's not how it works.

The Vector keeps a single array for storing the List data. The array (as all arrays are) is a fixed size. When that array runs out of space the Vector:

1) Creates a new array that is larger (~2x)
2) Copies all of the data from the current array into the new array
3) nulls out the old array reference and points it at the new array.

Therefore, there is only ever ONE array holding the list data that is used by the Vector. This is fundamentally different from the Linked List where there is a concept of list nodes and each node holds a pointer/reference to the object that it holds as well as to the next node in the list (and perhaps the previous node in the case of a doubly-linked list). There is no notion of a Node in an ArrayList or Vector.

Traversing a Linked List means following these references, one by one until you find the Object that you are looking for. Traversing an Array List means indexing directly into the array to find the Object.

As you can see from above, there are trade offs to the different List types. Adding an Object to an ArrayList is normally O(1) but degenerates to O(n) if the List has to constantly be resized (you are adding alot of elements). It is also always O(n) on element removal since you have to move all of the elements in the List over by one, on average. With a Linked List, however, insertion/deletion is always O(1) since the List never needs to be resized. Adding/removing a node is a matter of linking in or unlinking a node and is irrespective of the List size.

Hope this helps 8^)
 
A Vector is not the same to a linked list. A Vector is internally based upon an array. This means a Vector allocates chunks of memory from the heap at a time. If you add more elements than the internal array can hold then Vector creates a new array of twice the original size, copies the original's contents into the new array, and then scapes the original.

As Meadandale said, a get() on a Vector has a time complexity of O(1) which means it is simply delegating to the underlying array. A LinkedList, on the other hand, is O(N) for the same operation because it must go thru the entire list until the Nth element is reached.

But don't believe me... check out the source for Vector yourself and you will find that it is indeed implementing as an array. BTW, in case you didn't know, the source for all classes in the Java API is available with the JDK.
 
Fair do's lads. I'll get me coat !
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top