2016-12-06 57 views
2

對於此初始級別分配,我必須從文件和double[][]a設置2D多維數組,並向其應用幾種方法。就目前而言,我主要關心初始化數組。我試圖找出一種方法來獲取測試文件,讀取第一個int作爲行數,每行的第一個整數作爲每行的列數,每個double作爲數組的成員。Java 2D數組;來自文件輸入的變量行和列長度

public class MDArray 
    { 
     static int rowCount; 
     static int columnCount; 
     private static double[][] mdarray = new double[rowCount][columnCount]; 

    public MDArray(double[][] a) 
    { 
     mdarray = a; 
    } 

    public MDArray(String file) 
    { 
     Scanner input = null; 
     try 
     { 
      input = new Scanner(new FileInputStream("ragged.txt")); 
     } 
     catch (FileNotFoundException e) 
     { 
      System.out.println("File Not Found."); 
      System.exit(0); 
     } 
     while(input.hasNextDouble()) 
     { 
      rowCount = input.nextInt(); 
      for(int i = 0; i < rowCount; i++) 
      { 
       columnCount = input.nextInt(); 
       for(int j = 0; j < columnCount; j++) 
       { 
        double value = input.nextDouble(); 
        mdarray[i][j] = value; 
       } 
      } 
     } 
    } 

    public static boolean isRagged() 
    { 
     for(int i = 0; i < mdarray.length; i++) 
     { 
      int rowLength1 = mdarray.length; 
      for(int j = i + 1; j < mdarray.length; j++) 
      { 
       int rowLength2 = mdarray.length; 
       if(rowLength1 != rowLength2) 
       { 
        return true; 
       } 
      } 
     } 
     return false; 
    } 

    public static int getNumberOfRows() 
    { 
     int numRows = 0; 
     for(int i = 0; i < mdarray.length; i++) 
     { 
      numRows++; 
     } 
     return numRows; 
    } 

    public static int getNumberOfCols() 
    { 
     int numCols = 0; 
     for(int i = 0, j = i + 1; i < mdarray.length; i++) 
     { 
      for(int k = 0; k < mdarray[i].length; k++) 
      { 
       if(mdarray[i].length > mdarray[j].length) 
       { 
        numCols++; 
       } 
      } 
     } 
     return numCols; 
    } 

    public static double getValAt(int i, int j) 
    { 
     if(i > mdarray.length || j > mdarray[i].length) 
     { 
      double invalid = Double.NaN; 
      return invalid; 
     } 
     double valAt = mdarray[i][j]; 
     return valAt; 
    } 

    public static void sort(boolean byColumn) 
    { 
     if(isRagged() == true) 
     { 
      System.out.println("Ragged arrays cannot be sorted by column."); 
     } 
     else{ 
      for(int i = 0; i < mdarray.length; i++) 
      { 
       for(int j = 0; j < mdarray[i].length; j++) 
       { 
        for(int k = j + 1; k < mdarray[i].length; k++) 
        { 
         if(mdarray[i][j] < mdarray[i][k]) 
         { 
          double temp = mdarray[i][j]; 
          mdarray[i][k] = mdarray[i][j]; 
          mdarray[i][j] = temp; 
         } 
        } 
       } 
      } 
     } 
    } 

    public static int hamming(boolean byColumn) 
    { 
     int hamVal = 0; 
     if(isRagged() == true) 
     { 
      System.out.println("Ragged arrays cannot be sorted by column."); 
     } 
     else{ 
      for(int i = 0; i < mdarray.length; i++) 
      { 
       for(int j = 0; j < mdarray[i].length; j++) 
       { 
        for(int k = j + 1; k < mdarray[i].length; k++) 
        { 
         if(mdarray[i][j] < mdarray[i][k]) 
         { 
          double temp = mdarray[i][j]; 
          mdarray[i][k] = mdarray[i][j]; 
          mdarray[i][j] = temp; 
          hamVal++; 
         } 
        } 
       } 
      } 
     } 
     return hamVal; 
    } 

    public static double[] max() 
    { 
     double[] maxVal = new double[mdarray.length]; 
     for(int i = 0, j = i + 1; i < maxVal.length; i++) 
      { 
       for(int k = 0; k < mdarray[i].length; k++) 
       { 
        if(mdarray[i][k] > mdarray[j][k]) 
        { 
         maxVal = mdarray[i]; 
        } 
       } 
      } 
     return maxVal; 
    } 

    public String toString() 
    { 
     String arrayString = ""; 
     for(int i = 0; i < mdarray.length; i++) 
     { 
      for(int j = 0; j < mdarray[i].length; j++) 
      { 
       arrayString += ", " + mdarray[i][j]; 
      } 
      arrayString = arrayString + "/n"; 
     } 
     return arrayString; 
    } 
} 

這是我測試的MDArray(字符串文件)與文件:

2 4.1 8.9

5 9.5 2.0 7.3 2.1 8.9

3 1.3 5.2 3.4

我認爲問題是rowCountcolumnCount整數沒有初始化,但我不知道如何將它們初始化爲具有基本數組技能的可變長度。這也影響到其他構造函數。作爲一門初級課程,我不應該使用更先進的技術,如ArrayList。另外,我無法驗證方法是否正確,因爲我沒有一個數組來測試它們。

編輯:雖然我在答案中實現了許多建議,例如將所有內容更改爲非靜態和其他更改,但我仍然獲得NullPointerException對於該行mdarray[i][j] = input.nextDouble();.我認爲它必須與私有double[][] mdarray,這在分配規範中是必需的。現在我試圖找到一種方法來初始化它,以便它可以在後面的方法中被覆蓋。

+2

我能想到的唯一方法,實現它與一個正常的數組將會先讀取ll行,獲得最高的列數,然後初始化數組並再次讀取文件要添加值並將空值設置爲0或空 – XtremeBaumer

+0

您應該在讀取文件時分配數組。 –

+0

順便說一下,爲什麼所有的字段都是靜態的? –

回答

2

你必須初始化你的構造排列,因爲這是當你知道尺寸:

public MDArray(String file) 
{ 
    Scanner input = null; 
    try { 
     input = new Scanner(new FileInputStream("ragged.txt")); 
    } 
    catch (FileNotFoundException e) { 
     System.out.println("File Not Found."); 
     System.exit(0); 
    } 
    rowCount = input.nextInt(); 
    mdarray = new double[rowCount][]; // init the array 
    for(int i = 0; i < rowCount; i++) { 
     columnCount = input.nextInt(); 
     mdarray[i] = new double[columnCount]; // init the current row 
     for(int j = 0; j < columnCount; j++) { 
      mdarray[i][j] = input.nextDouble(); 
     } 
    } 

} 
1

你可以通過把行和列的量的前兩行的多維的初始化數組數組,如果我有10行12列,我可以做這樣的事情:

public void arrayStuff() { 
    File fileToRead = new File("YOUR LINK HERE"); 
    String[][] data; 


    try (BufferedReader reader = new BufferedReader(new FileReader(fileToRead))) { 

     String line; 
     data = new String[Integer.parseInt(reader.readLine())][Integer.parseInt(reader.readLine())]; 
     while((line = reader.readLine()) != null) { 
      // read the rest here.. 
     } 


    } catch (IOException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 
} 

我使用的是AutoCloseable(這就是爲什麼嘗試爲這些()之間的,但是這是爲了讓我不要之後不必關閉。

基本上,第一我讀行的數量也有,然後列的數量也有,所以如果我有這個文件:

10 
12 
a b c d e f g h i j 
a b c d e f g h i j 
a b c d e f g h i j 
a b c d e f g h i j 
a b c d e f g h i j 
a b c d e f g h i j 
a b c d e f g h i j 
a b c d e f g h i j 
a b c d e f g h i j 
a b c d e f g h i j 
a b c d e f g h i j 
a b c d e f g h i j 

這將會是能夠讀取所有這一切,因爲行和列的數量在文件中定義。

+0

這將工作在一般情況下,但考慮到作業的規格,我將無法使用此。我認爲該文件必須按照分配描述的方式進行設置,因爲不是每行都是相同的長度。 –

2

你不使用的字段rowCount時和信息columnCount:你可以刪除它們

的mdarray場應該是非靜態的,所以應該使用它們(如果它是一個工具類,你不會告發的方法「T有任何構造函數)

該陣列可以在讀取文件創建:

Scanner input = null; 
try 
{ 
    input = new Scanner(new FileInputStream("ragged.txt")); 
} 
catch (FileNotFoundException e) 
{ 
    System.out.println("File Not Found."); 
    System.exit(0); 
} 
int rowCount = input.nextInt(); 
mdarray = new double[rowCount][]; 
for(int i = 0; i < rowCount; i++) 
{ 
    int columnCount = input.nextInt(); 
    mdarray[i] = new double[columnCount]; 
    for(int j = 0; j < columnCount; j++) 
    { 
     double value = input.nextDouble(); 
     mdarray[i][j] = value; 
    } 
} 

方法getNumberOfRows()和getNumberOfCols()數要簡單得多:

public int getNumberOfRows() 
{ 
    return mdarray.length; 
} 

public int getNumberOfCols() { 
    int result = 0; 
    for (double[] col: mdarray) { 
     if (col.length > result) { 
      result = col.length; 
     } 
    } 
    return result; 
} 

在getValueAt()中,測試是錯誤的;它應該是:

if(i >= mdarray.length || j >= mdarray[i].length)