简体   繁体   中英

Why does my method that merges two array lists of strings together and sorts them keep returning multiple coppies of the strings at each index?

Here is my method. For this assignment, we were not allowed to use any of the "collections" methods like sort. My thought process goes as follows; first I make the new ArrayList by adding the two ArrayLists into it. Next, I do another for-loop and a value swap.

public static ArrayList<String> mergeStrings(ArrayList<String> list1, ArrayList<String> list2) {
        
        ArrayList<String> newList = new ArrayList<String>();
        
        for (int i = 0; i < list1.size(); i++) {
            for (int j = 0; j < list2.size(); j++) {
                
                newList.add(list1.get(i));
                newList.add(list2.get(j));
        }
    }
                
        String temp;
        
        for (int i = 0; i < newList.size(); i++) {
            for (int j = i + 1; j < newList.size(); j++) {
                
                if (newList.get(i).compareTo(newList.get(j)) > 0) {
                   
                    temp = newList.get(i);
                    newList.set(i, newList.get(j));
                    newList.set(j, temp);
                }
            }
        }
        return newList;
    }

My JUnit test case, and its results:

@Test
    public void testMergeStrings2() {
        ArrayList<String> a1 = new ArrayList<String>();
        a1.add("ant");
        a1.add("elephant");
        a1.add("camel");
        ArrayList<String> a2 = new ArrayList<String>();
        a2.add("deer");
        a2.add("bear");
        a2.add("frog");
        ArrayList<String> merged = new ArrayList<String>();
        merged.add("ant");
        merged.add("bear");
        merged.add("camel");
        merged.add("deer");
        merged.add("elephant");
        merged.add("frog");
        assertEquals(merged, ListUtilities.mergeStrings(a1, a2));
    }
java.lang.AssertionError: expected:<[ant, bear, camel, deer, elephant, frog]> but was:<[ant, ant, ant, bear, bear, bear, camel, camel, camel, deer, deer, deer, elephant, elephant, elephant, frog, frog, frog]>
    

As you can see from above, my test cases are obtaining multiple copies of each of the elements in the list. I'm sure that the problem lies within my for-loop disaster, but everything I do seems to make it much worse. Any help is appreciated!

The part where you are adding items from both the ArrayLists is causing the duplication of elements.

This is how the new ArrayList looks before you sort it:

["ant", "deer", "ant", "bear", "ant", "frog", "elephant", "deer", "elephant", "bear", "elephant", "frog", "camel", "deer", "camel", "bear", "camel", "frog"]

One possible way to avoid duplication of elements while adding them is

public static ArrayList<String> addElements(ArrayList<String> a1, ArrayList<String>     a2){
    ArrayList<String> newList = new ArrayList<String>();

    for(int i = 0; i<a1.size(); i++){
        newList.add(a1.get(i));
    }

    //another way to add
    for(String s : a2){
        newList.add(s);
    }
    
    //could also do newList.addAll(a1); newList.addAll(a2) if you were allowed to use collection methods
    return newList;
}

The problem is you are putting a for loop in another one. It should've been

for (int i=0;i<list1.size();i++){
   newList.get(list1.get(i));
}
for (int j=0;j<list2.size();j++){
   newList.get(list2.get(j));
}

Because currently, for every element in list2 you fetch the same element in list1 again (because the second for loop of j doesn't change i)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM