This week's book giveaway is in the Cloud/Virtualization forum.
We're giving away four copies of Grokking Bitcoin and have Kalle Rosenbaum on-line!
See this thread for details.
Win a copy of Grokking Bitcoin this week in the Cloud/Virtualization forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Frits Walraven
Bartenders:
  • Carey Brown
  • salvin francis
  • Claude Moore

Increasing Array Size  RSS feed

 
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?
 
lowercase baba
Posts: 12737
51
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.
 
Bartender
Posts: 1839
12
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That is SNEAKY! I like it!
 
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.
 
Bartender
Posts: 11445
18
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);
 
author
Posts: 23830
140
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
Bartender
Posts: 1839
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...

 
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
Bartender
Posts: 1839
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
 
Sheriff
Posts: 21717
102
Chrome Eclipse IDE Java Spring Ubuntu VI Editor 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
Bartender
Posts: 1839
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
Bartender
Posts: 1839
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
 
Marshal
Posts: 64109
215
  • 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
Bartender
Posts: 1839
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
Bartender
Posts: 1839
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
Marshal
Posts: 64109
215
  • 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
Bartender
Posts: 1839
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
Marshal
Posts: 64109
215
  • 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
Bartender
Posts: 1839
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
Marshal
Posts: 64109
215
  • 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
Posts: 12737
51
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.
 
I'm not dead! I feel happy! I'd like to go for a walk! I'll even read a tiny ad:
Create Edit Print & Convert PDF Using Free API with Java
https://coderanch.com/wiki/703735/Create-Convert-PDF-Free-Spire
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!