Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Increasing Array Size

 
Allen Hsia
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One of the questions that i have is to increase the array size with a method with the following header
public static int[] doubleCapacity(int[] list)

How is that even possible since when an array is created... the size is already fixed?
 
fred rosenberger
lowercase baba
Bartender
Posts: 12126
30
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you can't change the size of an array. but, you can create a new array that is twice the size, copy the data over, and return a reference to your new array.
 
Janeice DelVecchio
Saloon Keeper
Posts: 1809
12
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That is SNEAKY! I like it!
 
Siva Masilamani
Ranch Hand
Posts: 385
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can do it like this.But this still does not alter your array object it instead create a new one.



Hope it helps you.
 
Allen Hsia
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is what i have so far...
I this when i executed this program...

[I@e753

DId i make a mistake or is this a glitch?

 
Siva Masilamani
Ranch Hand
Posts: 385
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
IT looks fine.
 
Maneesh Godbole
Saloon Keeper
Posts: 11027
12
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Allen Hsia wrote:
DId i make a mistake or is this a glitch?

I suppose you want to check and confirm the array size with the System.out call.
For that you will have to use
System.out.println(doubleCapacity(ArrayList).length);
 
Henry Wong
author
Marshal
Pie
Posts: 21123
78
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Allen Hsia wrote:This is what i have so far...
I this when i executed this program...

[I@e753

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.

Henry
 
Janeice DelVecchio
Saloon Keeper
Posts: 1809
12
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Siva Masilamani wrote:IT looks fine.


It's not fine.

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.
[edit] 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.
Janeice

 
Allen Hsia
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think i did i correctly! Hopefully...

 
Hunter McMillen
Ranch Hand
Posts: 492
Firefox Browser Linux VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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[3]); I should get 0 or null but instead I get arrayIndexOutOfBounds

-Hunter
 
Janeice DelVecchio
Saloon Keeper
Posts: 1809
12
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're also only iterating only as many entries as the original length.... and one more hint..... you will need to change this line:



.... because you're not REALLY doing anything with its return value.

Janeice
 
Rob Spoor
Sheriff
Pie
Posts: 20531
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Allen Hsia wrote:

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.
 
Allen Hsia
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
do i set
doubleCapacity(originalList)


in the System.out.println?

I mean in my textbook, all it asks is to make a new array that doubles in size.

Gosh i feel stupid...
 
Janeice DelVecchio
Saloon Keeper
Posts: 1809
12
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Allen Hsia wrote:do i set
doubleCapacity(originalList)


in the System.out.println?

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!
Janeice
 
Allen Hsia
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
Allen Hsia
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
YAY IT WORKED!!!

the result i got was
1
2
0
0

the 0's are there because there aren't any values in the array right?

 
Janeice DelVecchio
Saloon Keeper
Posts: 1809
12
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
YAY!!!

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....

Great job!
Janeice
 
Campbell Ritchie
Sheriff
Pie
Posts: 48968
60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Janeice DelVecchio
Saloon Keeper
Posts: 1809
12
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I believe the 0's are for blank spots, yes....

It fills your array with ints, and if an int isn't initialized it automatically gives it a "0"........ pretty sure.

Janeice
 
Janeice DelVecchio
Saloon Keeper
Posts: 1809
12
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?

Janeice
 
Campbell Ritchie
Sheriff
Pie
Posts: 48968
60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Janeice DelVecchio
Saloon Keeper
Posts: 1809
12
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I agree. Completely.

And I'm thankful that Hunter brought those points up, because none of my textbooks are about Java 6.... so I had no idea. I learn something new in every thread I participate in.
 
Campbell Ritchie
Sheriff
Pie
Posts: 48968
60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And you are correct about int[] arrays being filled with 0s as a default. Object[] arrays are filled with nulls by default.
 
Janeice DelVecchio
Saloon Keeper
Posts: 1809
12
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What would I do without you telling me I'm right half the time and wrong the other half?
 
Campbell Ritchie
Sheriff
Pie
Posts: 48968
60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am right half the time and wrong half the time; I have to use Rob Prime and Joanne Neal to tell me which half is which
 
fred rosenberger
lowercase baba
Bartender
Posts: 12126
30
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There's nothing wrong with being right 1/2 the time and wrong the other 1/2. The trick is to know when you are which.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic