簡體   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

冗長的標題,我深表歉意。如果您能想到更好的標題,請告訴我!

我正在做的是嘗試創建一個ArrayLists的ArrayList並將ArrayLists逐一添加。 兩個AL<AL<I>> s表示我已被稱為三角形和正方形,我加入AL<I>s經由addToList()方法-添加AL<I>所謂的氣溫到適當的AL<AL<I>> temp似乎沒有問題,但是在運行整個方法figurateNumbers()之后,我的AL<AL<I>>s僅包含[98,70],這是最后要添加的溫度。 代碼如下:

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));
        }
}

無論是關於當前問題還是我對這些數據結構的使用,任何幫助都將不勝感激。

您並不是每次都在下面調用時創建新的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);
    }

您正在重復使用相同的溫度。 不要清除()它,每次在addToList()中創建一個新的(並且為了清楚起見使用局部變量)。

同樣,在addToList中,比所有String操作更容易將其除以100或取模(哎呀,從1000否回到100)。 例如

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

最后一個小建議:在您的figurateNumbers()循環中,您是否可以從34開始ii? 盡管速度提升可能不值得付出努力,但我內在的數學家還是想這樣做。 :-)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM