简体   繁体   English

Java添加ArrayList <Integer> 到ArrayList <ArrayList<Integer> &gt;替换ArrayLists的ArrayList的所有元素

[英]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. 我正在做的是尝试创建一个ArrayLists的ArrayList并将ArrayLists逐一添加。 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>> . 两个AL<AL<I>> s表示我已被称为三角形和正方形,我加入AL<I>s经由addToList()方法-添加AL<I>所谓的气温到适当的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. temp似乎没有问题,但是在运行整个方法figurateNumbers()之后,我的AL<AL<I>>s仅包含[98,70],这是最后要添加的温度。 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. 您并不是每次都在下面调用时创建新的temp实例,而是将相同的列表添加到要清除的列表中。请记住,它是添加的列表的引用。

 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(). 不要清除()它,每次在addToList()中创建一个新的(并且为了清楚起见使用局部变量)。

Also, in addToList, easier to just divide or modulo by 100 (oops, 1000? NO back to 100) than all that String manipulation. 同样,在addToList中,比所有String操作更容易将其除以100或取模(哎呀,从1000否回到100)。 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? 最后一个小建议:在您的figurateNumbers()循环中,您是否可以从34开始ii? Though the speed gain may not be worth the effort, the mathematician in me wants to do that. 尽管速度提升可能不值得付出努力,但我内在的数学家还是想这样做。 :-) :-)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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