简体   繁体   中英

Java adding ArrayList<Integer> to ArrayList<ArrayList<Integer>> replaces all elements of the ArrayList of ArrayLists

I apologize for the long title, please let me know if you can think of a better one!

What I am doing is trying to create an ArrayList of ArrayLists and adding ArrayLists to it one by one. The two AL<AL<I>> s that I have are called triangle and square, and I am adding AL<I>s via the addToList() method- adding the AL<I> called temp to the appropriate AL<AL<I>> . There doesn't seem to be a problem with temp, but after I run the entire method figurateNumbers(), my AL<AL<I>>s only contain [98,70], the last temp to be added. The code is below:

import java.util.ArrayList;
import java.util.Iterator;

    public class problem
    {
        public static ArrayList<ArrayList<Integer>> triangle = new ArrayList<ArrayList<Integer>>();
        public static ArrayList<ArrayList<Integer>> square = new ArrayList<ArrayList<Integer>>();
        public static ArrayList<Integer> temp = new ArrayList<Integer>();

        public static void figurateNumbers() 
        //Inserts into individual arraylists, numbers, all figurate numbers square : octagonal
        {
            for (int ii = 1; ii < 141; ii++) 
            {
                if ((ii * ii >= 1000) & (ii * ii < 10000))
                    addToList(ii * ii , square);
                if (((ii * ii + ii) / 2 >= 1000) & ((ii * ii + ii) / 2 < 10000))
                    addToList((ii * ii + ii) / 2 , triangle);
            }
}


    public static void addToList(int num, ArrayList<ArrayList<Integer>> list)
    //Splits the two parts of the number and inserts the arraylist into the proper arraylist
    {
        temp.clear();
        int numInt_one = Integer.parseInt(String.valueOf(num).substring(0,2));  
        int numInt_two = Integer.parseInt(String.valueOf(num).substring(2,4));  
        temp.add(numInt_one);
        temp.add(numInt_two);
        list.add(temp);
    }

    public static void main (String [] args) 
    {
        figurateNumbers();

        System.out.println(triangle.size());
        System.out.println(square.size());
    Iterator<ArrayList<Integer>> it = square.iterator();
    while(it.hasNext())
    {
        ArrayList<Integer> obj = it.next();
        System.out.println(obj);
    }
        System.out.println(triangle.get(25));
        }
}

Any help would be greatly appreciated, whether it's regarding the issue at hand or my use of these data structures.

You are not creating a new Instance of temp everytime you call below, the same list is added into the list, which you are clearing.Remember it is the reference of the list which is added.

 public static void addToList(int num, ArrayList<ArrayList<Integer>> list)
    //Splits the two parts of the number and inserts the arraylist into the proper arraylist
    {
      //  temp.clear();// this is the issue do below
        ArrayList<Integer> temp = new ArrayList<Integer>();
        int numInt_one = Integer.parseInt(String.valueOf(num).substring(0,2));  
        int numInt_two = Integer.parseInt(String.valueOf(num).substring(2,4));  
        temp.add(numInt_one);
        temp.add(numInt_two);
        list.add(temp);
    }

You are reusing the same temp all over. Do not clear() it, create a new one (and use a local variable for clarity) each time in addToList().

Also, in addToList, easier to just divide or modulo by 100 (oops, 1000? NO back to 100) than all that String manipulation. eg

int numInt_one = num / 100;
int numInt_two = num % 100;

One last minor suggestion: in your figurateNumbers() loop, can't you start ii at 34? Though the speed gain may not be worth the effort, the mathematician in me wants to do that. :-)

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