EI-01
EI-01

Reputation: 1095

storing a string and int in 2d array

Hello i am trying to understand 2d arrays in java. Basically i am trying to take cellular data from each country and display it like this:

Country 1983 1984 1985 USA 10 20 40 Mexico 2 3 1

Basically taking a string representing country and int representing actual stat number. i take the stats from 1983 to 1985.

My assumption not sure if i am right: Create an object of 2d array. 1 for string, 2nd for int. but get lost on the implementation and don't know if this is the right way to go or if anyone could help and make any suggestion will be grateful.

Any suggestion will be helpful.

below is my sample code implementation:

public class CellularData
 {
   private String []country;
   private double []stats;
   CellularData [][]array;
   private int year;

  public CellularData(int rows, int column, int year){
  this.country = new String[rows];
  this.stats = new double[column];
  array = new CellularData[country.length][stats.length];
  this.year = year;
   }
  public void insert(String country, double []num){
   //this is where I'm having the problem.
  //don't think  i am right.
     for(int i=0;i<array.length;i++)
        {
             array[i] = country;
          for(int j =0;j<array[i].length;j++)
            {
              array[i][j] = num[j];
            }
         }
   }




  //Below is my Test class
  public class TestCellularData(){
  public static void main(String []args){
   final double[] canada = {0,0,0.05,0.23,0.37,0.75,1.26};
              final double[] mexico = {0,0,0,0,0,0,0.01};
              final double[] usa = {0,0,0.14,0.28,0.5,0.83,1.39};
              int startingYear = 1983;
              CellularData datatable;
              int numRows = 3;
              int numColumns = canada.length;
              datatable = new CellularData(numRows, numColumns, startingYear);
              datatable.insert("canada", canadaPartial);
              datatable.insert("mexico", mexicoPartial);
              datatable.insert("usa", usaPartial);

              System.out.println(datatable);

Upvotes: 1

Views: 12409

Answers (3)

Tom
Tom

Reputation: 17567

Your provided source code has some small problems in it. First of all, you'll have to use an array of Object to store String and Double in it. An array of CellularData can't do that.

The second problems was your insert method. It wrote data to every row of the array and delete already stored data that way. To fix that you'll have to search for the first empty row first.

See the following code and the comments for more information.

public class CellularData {
  private Object[][] array; // <- use Object instead of CellularData

  public CellularData(int rows, int column, int year) {
    array = new Object[rows + 1][column + 1]; // <- +1 for the heading line and the country name column

    // write head line to array
    array[0][0] = "Country";
    for (int i = 1; i <= column; i++) {
      array[0][i] = year++;
    }
  }

  public void insert(String country, double[] num) {
    for (int i = 0; i < array.length; i++) {
      if (array[i][0] == null) { // <- search for an empty row to insert the data there
        insert(country, num, i);
        break;
      }
    }
  }

  private void insert(String country, double[] num, int row) {
    array[row][0] = country; // <- write the country to the first column
    for (int j = 1; j < array[row].length; j++) { // <- data starts at the second column
      array[row][j] = num[j - 1]; // <- -1 because the num array is one column shorter than 'array' (due to the country name column in 'array')
    }
  }

  public static void main(String[] args) {
    final double[] canada = { 0, 0, 0.05, 0.23, 0.37, 0.75, 1.26 };
    final double[] mexico = { 0, 0, 0,    0,    0,    0,    0.01 };
    final double[] usa =    { 0, 0, 0.14, 0.28, 0.5,  0.83, 1.39 };
    int startingYear = 1983;
    CellularData datatable;
    int numRows = 3;
    int numColumns = canada.length;
    datatable = new CellularData(numRows, numColumns, startingYear);
    datatable.insert("canada", canada);
    datatable.insert("mexico", mexico);
    datatable.insert("usa", usa);

    // print array content
    for (Object[] row : datatable.array) {
      for (Object cell : row) {
        System.out.print(cell + "\t");
      }
      System.out.println();
    }
  }
}

The test method prints

Country 1983    1984    1985    1986    1987    1988    1989    
canada  0.0     0.0     0.05    0.23    0.37    0.75    1.26    
mexico  0.0     0.0     0.0     0.0     0.0     0.0     0.01    
usa     0.0     0.0     0.14    0.28    0.5     0.83    1.39    

Upvotes: 1

Muhammad
Muhammad

Reputation: 7324

you can use an array of Object class like

Object[][] ob = {{"One", 1},{"Two", 2}, {"Three", 3}};

Note: to access the values you will need to cast the values to the required type (string does not need to be cast explicitly)

OR

you should use HashMap Like

HashMap<String, Integer> map = new HashMap<>();

use its put(String, Integer) method to insert the values and get(index) to access the values

Note: You cannot store the primative types in a HashMap but you will have to use Integer instead of int and later you can change it to int.

Upvotes: 0

Makoto
Makoto

Reputation: 106410

You could instead create an object that encapsulates these data points, and store that in a one-dimensional array instead. You could even make them sortable by the year by implementing Comparable.

public class CountryData implements Comparable<CountryData> {
    private final Integer year;
    private final String country;
    private final Integer dataPoint;

    public CountryData(Integer year, String country, Integer dataPoint) {
        this.year = year;
        this.country = country;
        this.dataPoint = dataPoint;
    }

    // presume getters defined

    public int compareTo(CountryData other) {
        return year.compareTo(other.getYear());
    }
}

Alternatively, if you're feeling particularly adventurous, you could experiment with Guava's Table as well.

Table<Integer, String, Integer> countryData = HashBasedTable.create();
countryData.put(1983, "USA", 10);
// and so forth

Upvotes: 0

Related Questions