File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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
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: 20526

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!
I agree. Here's the link:
subject: append objects to array
It's not a secret anymore!