Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Arrays. Making An array bigger???  RSS feed

 
Mr Ben
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok I have an array full of 10 objects. This array is called carArray..

Now I wish to add a new object to the array but am having trouble.

I started off thinking that I should create a tempoary array one index bigger then the current array, copy the information from the first array to the second array and then create the original array again but one bigger then copy the information from the temp array to the original... however when I try to create the original array again it says that it already exsists and wont compile..

here is my "copying array" code...


Car [] tempArray;
tempArray = new Car [carArray.length+1];
System.out.print(" Length = " + tempArray.length);

for (int x = 0; x <carArray.length; x++)

{
tempArray[x] = carArray[x];
}

System.out.print(tempArray[9].getMillage()); // to test objects been copied correctly...

Now this works fine I'm just not sure what to do now... I thought about maybe recreating the first array like this...

Car [] carArray;
carArray = new Car [tempArray.length];

but it already exsists and so wont compile,how do I increase the lenghth of the original array???

thanks alot...
 
Stefan Krompass
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

what if you try a class which implements the Collection-interface (for example ArrayList or Vector). The drawback is that you have to cast to Car if you want to get an object from your Collection (assumin you do not code with the beta version of JDK 1.5).

cu

Stefan
 
Mr Ben
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
if i change

Car [] carArray;
carArray = new Car [tempArray.length];

to just

carArray = new Car [tempArray.length];

Meh i'm pretty new.. the first line creates the array so i can't create it 2 times right?? but like any other variable I can change it afterwards.. hmm.. meh.. hope this works and won't bite me in the ass in about 500 lines time.
 
Ali Gohar
Ranch Hand
Posts: 572
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Use the java.util.Arraylist or java.util.Vector class to hold your Car objects. See the specification of these classes from http://java.sun.com. You don't need to specify the size of the list in these collections. Just store objects without any problem.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12542
48
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Once you create an array, you cannot change its size. Your idea of making a temp array should work, if you do it right. but i'd do it a little different...

instead of copying your original array to a temp, then making a bigger array, then copying back, can't you just make your temporary reference point to the original array?

i.e.

Car [] tempArray = carArray;

now you have a reference to the original array, so you can make your new carArray...

carArray = new Car[some size];

and then just copy your original array (which is referred to by the tempArray variable) back into carArray?

this save a LOT of copying, and will make your program much more efficient.

also, unless you have a pretty compelling reason, i wouldn't just increase the size by one each time. that can KILL your program when the array gets large enough. a common algorithm is that each time you run out of space, make the new array TWICE as big as the current size. so if you start with size 10, as soon as you need to put in element 11, make the new array 20. when THAT gets full, make the next one 40, etc.

this might not make much difference when your only dealing with 10 or 20 elements, but if you get up into the thousands or millions, your program will get so bogged down with all the copying you'll not be able to use it.

of course, if you use an arrayList or some other container, you don't even have to worry about this at all. the jvm will take care of re-sizing the arrayList for you (and i believe it does use this doubling of size algorithm).
[ May 17, 2004: Message edited by: fred rosenberger ]
 
fred rosenberger
lowercase baba
Bartender
Posts: 12542
48
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
re-reading your second post, i want to make a clarification...

the first line creates the array


this is not correct. the first line creates a REFERENCE to an array. it's like creating an an address card, with nothing written on it.

the second line does a couple of things. the "new Car [tempArray.length]" actually creates the array in memory somewhere. this is equivilent to building the house. then the "carArray =" assigns the memory address to your reference. this writes the address on the card.

what you can then do is erase what's on the address card, build a new house, and write down the new address. that's what "carArray = new Car [tempArray.length];" does.
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mr Ben,

Welcome to JavaRanch, again!

We ain't got many rules 'round these parts, but we do got one. Please change your display name to comply with The JavaRanch Naming Policy.

We'd like something other than a title for the displayed first name.

Thanks Pardner! Hope to see you 'round the Ranch!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!