This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes append objects to array Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "append objects to array" Watch "append objects to array" New topic

append objects to array

gon to

Joined: Jan 20, 2006
Posts: 18
hi all,
i got an array question. i'd like to create a new array and append nodes to it in a loop, like this:

Kj Reddy
Ranch Hand

Joined: Sep 20, 2003
Posts: 1704
You can add the following line below inside the loop:

allChildren[index] = aNode;

You need to give integer value for index.
Henry Wong

Joined: Sep 28, 2004
Posts: 18546

Arrays don't keep track of the last used members -- so it doesn't have a concept of append. You can set any member as such...

allChildren[some_index] = anode;

You have two options. You can keep track of the last used location yourself, or you can use one of the collections classes that does do it -- such as ArrayList or Vector.


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
gon to

Joined: Jan 20, 2006
Posts: 18
thats it, thanks very much!
Consider Paul's rocket mass heater.
subject: append objects to array
Similar Threads
Line Feed Carriage Return problem
Static Doubly Linked Lists....
filtering N number of leaf nodes per parent node
how to add a new node to already existing node but i dont know the parent node
How to "flatten" a Ruby array