簡體   English   中英

在2D數組中存儲字符串和整數

[英]storing a string and int in 2d array

您好,我試圖了解Java中的2D數組。 基本上,我試圖從每個國家/地區獲取蜂窩數據並像這樣顯示:

國家1983 1984 1985美國10 20 40墨西哥2 3 1

基本上采用代表國家的字符串和代表實際統計數字的整數。 我采用1983年至1985年的統計數據。

我的假設不確定我是否正確:創建2d數組的對象。 1用於字符串,第二用於int。 但會迷失在實現上,並且不知道這是否是正確的方法,或者是否有人可以幫助並提出任何建議將不勝感激。

任何建議都會有所幫助。

以下是我的示例代碼實現:

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

您提供的源代碼中有一些小問題。 首先,您必須使用Object數組在其中存儲StringDouble CellularData數組無法做到這一點。

第二個問題是您的insert方法。 它將數據寫入數組的每一行,並以此方式刪除已存儲的數據。 要解決此問題,您必須先搜索第一個空行。

有關更多信息,請參見以下代碼和注釋。

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

測試方法打印

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    

相反,您可以創建一個封裝這些數據點的對象,並將其存儲在一維數組中。 您甚至可以通過實施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());
    }
}

另外,如果您特別喜歡冒險,也可以嘗試使用番石榴

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

您可以使用Object類的數組

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

注意:要訪問值,您需要將值轉換為所需的類型(不需要顯式轉換字符串)

要么

你應該使用HashMap Like

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

使用其put(String, Integer)方法插入值,並使用get(index)訪問值

注意:您不能將基本類型存儲在HashMap但是必須使用Integer而不是int,以后可以將其更改為int。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM