This week's book giveaway is in the OCPJP forum.
We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Joining two File[] arrays Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Joining two File[] arrays" Watch "Joining two File[] arrays" New topic

Joining two File[] arrays

mandlar suurla
Ranch Hand

Joined: Jun 11, 2008
Posts: 67

How to make it i am losing my mind. Cant be done like this

file = oneArray + otherArray;
Jelle Klap

Joined: Mar 10, 2008
Posts: 1795

What exactly are you trying to accomplish?
If you simply want to merge two arrays, not caring about duplicates, I think making use of System.arraycopy() will probably yield the best performance.

Build a man a fire, and he'll be warm for a day. Set a man on fire, and he'll be warm for the rest of his life.
mandlar suurla
Ranch Hand

Joined: Jun 11, 2008
Posts: 67

k the actual problem is making FileSystemTableModel so it would display the system in table. The file list i get with File class. And two arrays are because i want the folders listed first and files after folders.
Rob Spoor

Joined: Oct 27, 2005
Posts: 19725

There is another way of solving this: just list them all (listFiles), then use Arrays.sort to sort them. You will need a Comparator for that:

This will also help you with the sorting of your files. You cannot assume that listFiles will return your files in alphabetical order; some operating systems use creation date sorting.

Of course this method is O(n log n) against the O(n) of listing the files twice, then merging, but unless there are a lot of files / folders in each folder you won't notice the difference.

By the way, that code came out of my own FileTreeModel, so it has proven itself already in existing code
[ June 11, 2008: Message edited by: Rob Prime ]

How To Ask Questions How To Answer Questions
jQuery in Action, 2nd edition
subject: Joining two File[] arrays