Curious Tanned
Curious Tanned

Reputation: 35

Why is my original arraylist amended even though I only make changes to my ArrayList within the method without changing the orignal ArrayList

import java.util.ArrayList;

public class BubbleSort {

  // the sort method takes in an ArrayList of Strings, sorts 
  // them in ascending number of characters, and returns a new
  // ArrayList of Strings which is already sorted. This method 
  // does NOT modify the original ArrayList passed in.

  public ArrayList<String> sort(ArrayList<String> a){
    ArrayList<String> sortingList = new ArrayList<String>();
    sortingList = a;
    String test = "";
    String test2 = "";
    int length = 0;
    int length2 = 0;
    for(int j =0; j<a.size(); j++){
        for (int i =0; i<sortingList.size()-1; i++){
            test = a.get(i);
            test2 = a.get(i+1);
            length = test.length();
            length2 = test2.length();
            if(length2<length){
                sortingList.set(i,test2);
                sortingList.set(i+1,test);
            }
        }
    }
    return sortingList;
 }
}

=================================MAIN METHOD=====================================

import java.util.ArrayList;
import java.util.Scanner;

public class BubbleSortTest {

  public static void main(String[] args) {
    ArrayList<String> inputs = new ArrayList<String>();

    // get inputs from user
    Scanner sc = new Scanner(System.in);
    System.out.print("Enter number of Strings to enter: ");
    int no = sc.nextInt();
    sc.nextLine(); // clears buffer in Scanner

    for (int i = 0; i < no; i++){
      System.out.print("Enter String number " + i + ": ");
      inputs.add(sc.nextLine());  // add input into ArrayList
    }

    // invoke the sort method to see if it works
    BubbleSort bs = new BubbleSort();
    ArrayList<String> sortedInputs = bs.sort(inputs);

    // print out the Strings in sortedInputs
    System.out.println("Sorted sequence:");
    for (int i = 0; i < sortedInputs.size(); i++){
      System.out.println(sortedInputs.get(i));
    }

    // print out the Strings in the original inputs
    System.out.println("Original sequence:");
    for (int i = 0; i < inputs.size(); i++){
      System.out.println(inputs.get(i));
    }
  }
}

input example

121234256464534

1123123

123141243124124

123

my sorting sequence & original sequence are both amended in ascending sequenceenter image description here even though I created a new arraylist to return while ensuring I did not make any changes to the original one.

Thanks in advance

Upvotes: 1

Views: 338

Answers (3)

Melquiades
Melquiades

Reputation: 8598

As Axel pointed, use:

ArrayList<String> sortingList = new ArrayList<String>(a);

With that change, in your BubbleSort class, you are still referencing strings from "a" array, hence they are not being sorted properly. To fix, change:

test = a.get(i);
test2 = a.get(i+1);

to

test = sortingList.get(i);
test2 = sortingList.get(i+1);

Full code:

public ArrayList<String> sort(ArrayList<String> a){

    //As Axel pointed, use:
    ArrayList<String> sortingList = new ArrayList<String>(a);

    String test = "";
    String test2 = "";
    int length = 0;
    int length2 = 0;

    for(int j =0; j<sortingList.size(); j++){
        for (int i =0; i<sortingList.size()-1; i++){

            //reference "sortingList" array instead of "a" array
            test = sortingList.get(i);
            test2 = sortingList.get(i+1);

            length = test.length();
            length2 = test2.length();

            if(length2<length){
                sortingList.set(i,test2);
                sortingList.set(i+1,test);
            }
        }
    }
    return sortingList;
}

Upvotes: 1

Deepak Bhatia
Deepak Bhatia

Reputation: 6276

Just remove this line from your code,

   ArrayList<String> sortingList = new ArrayList<String>();
    sortingList = a; //REMOVE THIS LINE

As you are assigning a new arraylist object just before but in the next line you are storing the reference to original object in sortingList object.

If you want to copy all the elements to sortingList then look at Evans Post but in your example you do not need it as you are assigning the test and test2 variables from original Array supplied.

Upvotes: 1

Evans
Evans

Reputation: 1599

sortingList = a;

Both references, sortingList and a are pointing to the same ArrayList object

Check this question to see how to see how to clone the list: How to clone ArrayList and also clone its contents?

Upvotes: 5

Related Questions