I am trying to \"combine\" two arrayLists, producing a new arrayList that contains all the numbers in the two combined arrayLists, but without any duplicate elements and the
Your second for loop should have j++ instead of i++
Firstly remove duplicates:
arrayList1.removeAll(arrayList2);
Then merge two arrayList:
arrayList1.addAll(arrayList2);
Lastly, sort your arrayList if you wish:
collections.sort(arrayList1);
In case you don't want to make any changes on the existing list, first create their backup lists:
arrayList1Backup = new ArrayList(arrayList1);
Perhaps my nested for loops being used incorrectly?
Hint: nested loops won't work for this problem. A simple for loop won't work either.
You need to visualize the problem.
Write two ordered lists on a piece of paper, and using two fingers to point the elements of the respective lists, step through them as you do the merge in your head. Then translate your mental decision process into an algorithm and then code.
The optimal solution makes a single pass through the two lists.
Add elements in first arraylist
ArrayList<String> firstArrayList = new ArrayList<String>();
firstArrayList.add("A");
firstArrayList.add("B");
firstArrayList.add("C");
firstArrayList.add("D");
firstArrayList.add("E");
Add elements in second arraylist
ArrayList<String> secondArrayList = new ArrayList<String>();
secondArrayList.add("B");
secondArrayList.add("D");
secondArrayList.add("F");
secondArrayList.add("G");
Add first arraylist's elements in second arraylist
secondArrayList.addAll(firstArrayList);
Assign new combine arraylist and add all elements from both arraylists
ArrayList<String> comboArrayList = new ArrayList<String>(firstArrayList);
comboArrayList.addAll(secondArrayList);
Assign new Set for remove duplicate entries from arraylist
Set<String> setList = new LinkedHashSet<String>(comboArrayList);
comboArrayList.clear();
comboArrayList.addAll(setList);
Sorting arraylist
Collections.sort(comboArrayList);
Output
A
B
C
D
E
F
G
I'm not sure why your current code is failing (what is the Exception you get?), but I would like to point out this approach performs O(N-squared). Consider pre-sorting your input arrays (if they are not defined to be pre-sorted) and merging the sorted arrays:
http://www.algolist.net/Algorithms/Merge/Sorted_arrays
Sorting is generally O(N logN) and the merge is O(m+n).
**Add elements in Final arraylist,**
**This will Help you sure**
import java.util.ArrayList;
import java.util.List;
public class NonDuplicateList {
public static void main(String[] args) {
List<String> l1 = new ArrayList<String>();
l1.add("1");l1.add("2");l1.add("3");l1.add("4");l1.add("5");l1.add("6");
List<String> l2 = new ArrayList<String>();
l2.add("1");l2.add("7");l2.add("8");l2.add("9");l2.add("10");l2.add("3");
List<String> l3 = new ArrayList<String>();
l3.addAll(l1);
l3.addAll(l2);
for (int i = 0; i < l3.size(); i++) {
for (int j=i+1; j < l3.size(); j++) {
if(l3.get(i) == l3.get(j)) {
l3.remove(j);
}
}
}
System.out.println(l3);
}
}
Output : [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]