Lizzy Lu
Lizzy Lu

Reputation: 25

How to get and store an index from a 2D arraylist

I have a 2D array list in which each row has teams 1-4 randomly shuffled. I have a separate arraylist for each team in which I want to store its index. For example, if my 2D arraylist looked like:

2, 3, 1, 4

2, 1, 4, 3

4, 2, 1, 3

My arraylist for team1 indexes should be [2,1,2], team 2 should be [0, 0, 1], team 3 should be [1 ,3, 3], and team 4 should be [3, 2, 0].

However, I'm having trouble with implementing a loop that can identify the index of each number and add it to its corresponding arraylist, then move to the next row. I end up with a value of -1 (meaning that the requested number did not exist). May someone take a look at what I have so far and suggest some help?

Just a little background (because I have some other stuff in here): the arraylist I am storing the shuffled numbers in is dynamic. The shuffled numbers are only added if the temperature input is above 32. If the temperature input is below 32 for three times in a row, then the loop terminates.

package practice;
import java.util.Scanner;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Arrays;
import java.util.List;

public class everythingTogether {
  public static void main(String[] args) {
    //Create Arrays
    //Store Temperatures
    ArrayList < Double > allTemperatures = new ArrayList < > ();
    ArrayList < Double > daysPlayed = new ArrayList < > ();
    List < Integer[] > teamMatches = new ArrayList < > ();

    //make array list for team index (0,1 home| 2,3 away)
    ArrayList < Integer > homeOrAway1 = new ArrayList < > ();
    ArrayList < Integer > homeOrAway2 = new ArrayList < > ();
    ArrayList < Integer > homeOrAway3 = new ArrayList < > ();
    ArrayList < Integer > homeOrAway4 = new ArrayList < > ();




    //start loop for temp and datat collection
    for (int j = 0;; j++) {
      Integer[] teamNums = new Integer[] {
        1,
        2,
        3,
        4
      };

      //scanner for temperature
      Scanner getTemp = new Scanner(System.in);
      System.out.println("What is today's temperature?");
      double temp = getTemp.nextDouble();
      allTemperatures.add(temp);

      //checking temperature and if can play
      if (j < 2) {
        if (temp < 32) {
          System.out.println("It is too cold to play.");
        } else {
          Collections.shuffle(Arrays.asList(teamNums));
          teamMatches.add(teamNums);
          daysPlayed.add(temp);
        }
      }

      //after day 0 and 1 check if season can be over
      else if (j >= 2) {
        if (temp < 32) {
          System.out.println("It is too cold to play.");

          //check if season over
          double day1Temp = allTemperatures.get(j);
          double day2Temp = allTemperatures.get((j - 1));
          double day3Temp = allTemperatures.get((j - 2));
          if (day1Temp < 32 & day2Temp < 32 & day3Temp < 32) {
            System.out.println("The season is over");
            break;
          }
        } else {
          Collections.shuffle(Arrays.asList(teamNums));
          teamMatches.add(teamNums);
          daysPlayed.add(temp);
        }
      }
    }

    System.out.println("_______________________");

    //get home teams and away teams

    for (int i = 0; i < teamMatches.size(); i++) {

      int team1Status = teamMatches.indexOf(1);
      int team2Status = teamMatches.indexOf(2);
      int team3Status = teamMatches.indexOf(3);
      int team4Status = teamMatches.indexOf(4);

      homeOrAway1.add(team1Status);
      homeOrAway2.add(team2Status);
      homeOrAway3.add(team3Status);
      homeOrAway4.add(team4Status);
    }


    System.out.println(homeOrAway1);
    System.out.println(homeOrAway2);
    System.out.println(homeOrAway3);
    System.out.println(homeOrAway4);


  }
}

Upvotes: 1

Views: 874

Answers (1)

PhaseRush
PhaseRush

Reputation: 350

I believe the error you're getting is due to this loop:

for (int i = 0; i < teamMatches.size(); i++) {

  int team1Status = teamMatches.indexOf(1);
  int team2Status = teamMatches.indexOf(2);
  int team3Status = teamMatches.indexOf(3);
  int team4Status = teamMatches.indexOf(4);

  homeOrAway1.add(team1Status);
  homeOrAway2.add(team2Status);
  homeOrAway3.add(team3Status);
  homeOrAway4.add(team4Status);
}

Keep in mind that this is your declaration:

List < Integer[] > teamMatches

teamMatches is a List of Integer array. Therefore, when you call:

int team1Status = teamMatches.indexOf(1); 

you're not getting what you think you are. This is what teamMatches actually is: Representation of teamMatches

What you actually want is this:

for (int i = 0; i < teamMatches.size(); i++) {

        Integer[] ints = teamMatches.get(i);
        for (int j = 0; j < ints.length; j++) {
            if (ints[j] == 1) homeOrAway1.add(j);
            else if (ints[j] == 2) homeOrAway2.add(j);
            else if (ints[j] == 3) homeOrAway3.add(j);
            else if (ints[j] == 4) homeOrAway4.add(j);
        }
    }

Edit: I'm going to add an explanation so hang on tight.

teamMatches is a List of Array of Integer. The array is a representation of 1,2,3,4 (scrambled), which essentially a representation for a game. Since there is a List of this, teamMatches is a List of Games.

Integer[] ints = teamMatches.get(i) is what you actually want. Now, the variable ints is a single game. From this single game, you can extract information regarding the positions of each team (not sure about terminology, sorry).

Rest of the inner loop should be self explanitory. It loops through the Array of Integer (position of teams) and adds the current index to the homeOrArray1/2/3/4 depending on the value at the index.

NOTE: I strongly recommend never using an Array of Integer, unless there is a very good reason to. I believe what you actually want is a List<Integer> because now you can just do ints.indexOf(1/2/3/4); instead of a for loop.

Obviously, I don't know the spec of your program, but assuming that you're allowed to redefine teamMatches, the following code is much cleaner imo.

List<List<Integer>> teamMatches = new ArrayList<>();

...

Now your entire for loop can be replaced by :

for (List<Integer> teamPositions : teamMatches) {
        homeOrAway1.add(teamPositions.indexOf(1));
        homeOrAway2.add(teamPositions.indexOf(2));
        //...           
    }

Upvotes: 1

Related Questions