Allen Hsia wrote:This is what i have so far...
I this when i executed this program...
DId i make a mistake or is this a glitch?
First, instantiating a new array that is double in size, does exactly that. The new array is still not initialized (have zero as all the element values). So.... Don't you also want to copy the values from the original array to the new array too?
Second, you can't just print an array. All it does is print that it is an array -- along with some id for the array. If you would like to print the elements of the array, you'll need a loop.
1. It's confusing to name your variable ArrayList.
1a) don't use the name of another type of array
1b) start the name with a lowercase letter and use camelCase
2. The reason it's not outputting correctly is because you're not iterating through the array. When you do this, I think you may need to watch out for null values.
 3. Henry's right..... copy your values into the new array -- missed this one
try this first:
if you get a null pointer exception you'll need an if statement so it only outputs actual values.
That is the correct format but you aren't doubling the size of the array. You are just copying the same size over.
If I entered System.out.println(originalList); I should get 0 or null but instead I get arrayIndexOutOfBounds
"If the facts don't fit the theory, get new facts" --Albert Einstein
Although that code will indeed copy all your array contents, you should use the API to do it for you: That is a) shorter, and b) faster because it will be executed by native code in the JVM itself.
As for increasing the size of an array, since Java 6 you can simply use Arrays.copyOf(list, list.length * 2) and the API will do all the hard work for you. Except if this is a homework assignment of course.
I mean in my textbook, all it asks is to make a new array that doubles in size.
Gosh i feel stupid...
Not exactly. What you need to do is catch the return value in a new variable so you can use it. Right now, the method sends back a new array, but you can't use it.
How about this?
Then, you have the newArray that you can iterate through, based on the newArray.length. fred rosenberger said above when this idea originally came up, you can't double the size of an array, but you can create a NEW array with a length of twice the size of the original, and copy the data to it.
Don't feel stupid, you're on the right track!
Joined: Oct 29, 2009
int newArray = doubleCapacity(originalList);
Ohhh You're supposed to add the bracket
I forgot adding that and the error i got was incompatible types found int but expected int.
That makes sense now. I could now actually do the other 3 homework problems i'm stuck on.
Thank you everyone!
Joined: Oct 29, 2009
YAY IT WORKED!!!
the result i got was
the 0's are there because there aren't any values in the array right?
I find that it's easy to forget that learning to interpret compiler and runtime error messages is part of learning the language. Don't take it for granted! The compiler is sending you a line number, a carat telling you what it thinks is wrong, and a message telling you why. USE that information to break down the problem...
"Incompatible types" and "found int" and "expected int" are clues you can use to solve the mystery....
Are you still using the doubleCapacity() method as you wrote it before? Or did you take Hunter McMillen's hint?
The names of your methods are less than optimal; "doubleCapacity" doesn't double the capacity of anything. It would probably be better to call it "getDoubleSizedArray()". And it's probably better to use names like "array" for your arrays rather than list because there is something different called a List.
Campbell Ritchie wrote:Are you still using the doubleCapacity() method as you wrote it before? Or did you take Hunter McMillen's hint?
The code is there, Campbell
I think the point of the project was to CREATE the method..... otherwise using the JVM as Hunter said would be a FAR BETTER approach, right?
Joined: Oct 13, 2005
What Hunter pointed out was that the method (as written) returns an array the same size. If it is a beginner's class exercise, then it is probably best to create the method; in "real life" one would use the API methods Rob quoted.