简体   繁体   中英

Spliting ArrayList into 2D ArrayList

I have the following code:

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

It is supposed to split an ArrayList (costs) and put values into a 2D ArrayList (twoDArrayList). This almost works except the code just puts the same values into each ArrayList within twoDArrayList. Here is the output:

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.

I need it to move so that the arrayList is :

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

Thanks!

Here is the full code:

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

}
}

Do you want 2 or more column in one value?

example

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

All the above in the 2d Array? If so.

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

To add a value u need to do this

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

To get values

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

Need more information on what your input is.

Do you want something like this?

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

or maybe this is what you want

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

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