File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Java in General and the fly likes Merge two arrays Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Merge two arrays " Watch "Merge two arrays " New topic

Merge two arrays

Hui Zhao
Ranch Hand

Joined: Jul 09, 2007
Posts: 116
Suppose a1 and a2 are two dimensional arrays

My goal is to append the second row according to the first row's matchness.
Finally if
merge the first row of a1 and a2 should get

I have three days but no achievement.
Here is the code.
Ernest Friedman-Hill
author and iconoclast

Joined: Jul 08, 2003
Posts: 24199

Consider how you could use a Map (for your purposes, a java.util.LinkedHashMap would be a wise choice) to store the relationship between the first and second strings. Since a Map automatically eliminates duplicate keys, and since LinkedHashMap stores the keys in insertion order, it would do virtually all of the work for you. You'd just add all the pairs, allocate new arrays the same size as the map, then iterate over the map and populate the two arrays.

[Jess in Action][AskingGoodQuestions]
Hui Zhao
Ranch Hand

Joined: Jul 09, 2007
Posts: 116
I am not familiar with using Map skills.
Would you please provide me a piece of code?

It is sorta covered in the JavaRanch Style Guide.
subject: Merge two arrays
It's not a secret anymore!