File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Increasing Array Size Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Increasing Array Size" Watch "Increasing Array Size" New topic
Author

Increasing Array Size

Allen Hsia
Greenhorn

Joined: Oct 29, 2009
Posts: 20
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

Joined: Oct 02, 2003
Posts: 11475
    
  16

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.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Janeice DelVecchio
Saloon Keeper

Joined: Sep 14, 2009
Posts: 1726
    
  12

That is SNEAKY! I like it!


When you do things right, people won't be sure you've done anything at all.
Siva Masilamani
Ranch Hand

Joined: Sep 19, 2008
Posts: 385
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.


SCJP 6,SCWCD 5,SCBCD 5

Failure is not an option.
Allen Hsia
Greenhorn

Joined: Oct 29, 2009
Posts: 20
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

Joined: Sep 19, 2008
Posts: 385
IT looks fine.
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10519
    
    9

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);


[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18978
    
  40

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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Janeice DelVecchio
Saloon Keeper

Joined: Sep 14, 2009
Posts: 1726
    
  12

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

Joined: Oct 29, 2009
Posts: 20
I think i did i correctly! Hopefully...

Hunter McMillen
Ranch Hand

Joined: Mar 13, 2009
Posts: 492

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


"If the facts don't fit the theory, get new facts" --Albert Einstein
Janeice DelVecchio
Saloon Keeper

Joined: Sep 14, 2009
Posts: 1726
    
  12

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

Joined: Oct 27, 2005
Posts: 19759
    
  20

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Allen Hsia
Greenhorn

Joined: Oct 29, 2009
Posts: 20
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

Joined: Sep 14, 2009
Posts: 1726
    
  12

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

Joined: Oct 29, 2009
Posts: 20
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

Joined: Oct 29, 2009
Posts: 20
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

Joined: Sep 14, 2009
Posts: 1726
    
  12

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

Joined: Oct 13, 2005
Posts: 39788
    
  28
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

Joined: Sep 14, 2009
Posts: 1726
    
  12

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

Joined: Sep 14, 2009
Posts: 1726
    
  12

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

Joined: Oct 13, 2005
Posts: 39788
    
  28
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

Joined: Sep 14, 2009
Posts: 1726
    
  12

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

Joined: Oct 13, 2005
Posts: 39788
    
  28
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

Joined: Sep 14, 2009
Posts: 1726
    
  12

What would I do without you telling me I'm right half the time and wrong the other half?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39788
    
  28
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

Joined: Oct 02, 2003
Posts: 11475
    
  16

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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Increasing Array Size