将ArrayList拆分为2D ArrayList

时间:2014-07-30 19:59:29

标签: java arraylist

我有以下代码:

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(cost)并将值放入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));

}
}

2 个答案:

答案 0 :(得分:0)

您想在一个值中包含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

以上所有2d阵列?如果是的话。

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

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

答案 1 :(得分:-1)

你想要这样的东西吗?

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