【问题标题】:Spliting ArrayList into 2D ArrayList将 ArrayList 拆分为 2D ArrayList
【发布时间】:2014-07-30 19:59:29
【问题描述】:

我有以下代码:

for (int i = 0; i < costs.size(); i++){
        System.out.println(twoDArrayList);
        for (int j = 0; j < cities.size(); j++){
            if (i==0 || i%cities.size() != 0){
                twoDArrayList.get(j).add(costs.get(i));
            } 
        }
    }

它应该拆分一个 ArrayList(成本)并将值放入一个 2D ArrayList(twoDArrayList)。这几乎可以工作,只是代码只是将相同的值放入 twoDArrayList 中的每个 ArrayList。这是输出:

begin execution
[Seattle, NewOrleans, LosAngeles, Tucson, Chicago, Miami, Omaha]
[[], [], [], [], [], [], []]
[[0], [0], [0], [0], [0], [0], [0]]
[[0, 2706], [0, 2706], [0, 2706], [0, 2706], [0, 2706], [0, 2706], [0, 2706]]
[[0, 2706, 1136], [0, 2706, 1136], [0, 2706, 1136], [0, 2706, 1136], [0, 2706, 1136],        [0, 2706, 1136], [0, 2706, 1136]]
...etc.

我需要它移动,以便 arrayList 是:

[0][2706][1136][etc...]

谢谢!

这里是完整的代码:

private static ArrayList<String> cities;
private static ArrayList<Integer> costs;
private static ArrayList<ArrayList<Integer>> twoDArrayList;


public TSP(Scanner inFile) {
    cities = new ArrayList<String>();
    costs = new ArrayList<Integer>();
    String newCities;
    newCities = (inFile.nextLine());
    String[] stringArray = newCities.split(" ");
    for (int i = 0; i < stringArray.length; i++){
        cities.add(stringArray[i]);
    }
    System.out.println(cities);


    twoDArrayList = new ArrayList<ArrayList<Integer>>();



    for (int i = 0; i < cities.size(); i++){
        twoDArrayList.add(new ArrayList<Integer>()); 
    }


    while (inFile.hasNextInt()){
        int newCost = inFile.nextInt();
        costs.add(newCost);
    }

    for (int i = 0; i < costs.size(); i++){
        System.out.println(twoDArrayList);
        for (int j = 0; j < cities.size(); j++){
            if (i==0 || i%cities.size() != 0){
                twoDArrayList.get(j).add(costs.get(i));
            } 
        }
    }

    System.out.println(twoDArrayList.get(0).get(0));
    System.out.println(twoDArrayList.get(0).get(1));
    System.out.println(twoDArrayList.get(0).get(2));
    System.out.println(twoDArrayList.get(2).get(0));
    System.out.println(twoDArrayList.get(2).get(1));
    System.out.println(twoDArrayList.get(2).get(2));

}
}

【问题讨论】:

  • 我不太了解初始数组列表中的内容以及二维数组列表中应该包含的内容,知道类型(列表的初始化)也会很好。
  • 我编辑了原始问题。
  • 请显示以下内容:原始arraylist内容;预期的输出。

标签: java arraylist


【解决方案1】:

您希望一个值包含 2 列或更多列吗?

例子

<cities>    <costs>    <distance>
Seattle     58         594
NewOrleans  74         434
LosAngeles  42         43
Tucson      43         456
Chicago     20         248
Miami       90         109
Omaha       45         85

以上所有都在二维数组中?如果是这样。

ArrayList twoDArrayList= new ArrayList<List>();
twoDArrayList.add(new ArrayList<String>());  //Cities
twoDArrayList.add(new ArrayList<Integer>()); //Costs
twoDArrayList.add(new ArrayList<Integer>()); //Distance

要添加一个值,你需要这样做

list.get(0).add("Enter a place"); //Adding a location
list.get(1).add(50);              //Adding a cost for the location you just added
list.get(2).add(534);             //Adding distance for the location you just added

获取值

list.get(0).get(i);   //gets location at index i
list.get(1).get(i);   //gets cost at index i
list.get(2).get(i);   //gets cost at index i

需要有关您的输入内容的更多信息。

【讨论】:

    【解决方案2】:

    你想要这样的东西吗?

        int j = 0;
        for (int i = 0; i < costs.size(); ++i){
                System.out.println(twoDArrayList);
                    if ((i+1)%cities.size() != 0 && j < cities.size()){
                        twoDArrayList.get(j).add(costs.get(i));
                    }
                    else {
                        ++j;
                    }
                }
            }
    

    也许这就是你想要的

        int j = 0;
        int count = 1;
        for (int i = 0; i < costs.size(); ++i){
            if (count  % cities.size() != 0 && j < cities.size()){
                twoDArrayList.get(j).add(costs.get(i));
                ++count;
            }
            else {
                count = 2;
                ++j;
                if (j < cities.size()) twoDArrayList.get(j).add(costs.get(i));
            }
        }
    
        System.out.println(twoDArrayList);
    

    【讨论】:

    • 这很聪明,我不知道你可以将大小检查放在 if 语句中!这几乎可行,我只需要弄清楚如何获得其余的值。我会玩它的,谢谢!
    • 当我到达索引 1 时,事情就崩溃了。我得到一个 IndexOutOfBoundsException。
    猜你喜欢
    • 2014-08-17
    • 2013-12-13
    • 1970-01-01
    • 2014-12-29
    • 1970-01-01
    • 2011-02-23
    • 1970-01-01
    • 2018-08-16
    • 2012-05-28
    相关资源
    最近更新 更多