2016-12-06 8 views
2

このイントロレベルの割り当てでは、ファイルとdouble[][]から2D多次元配列を設定し、それらにいくつかのメソッドを適用する必要があります。今のところ、私は主に配列を初期化するだけです。私はテストファイルを取る方法を見つけようとしています。行の数として最初のintを、行ごとの列の数として各行の最初の整数を、配列のメンバーとしてそれぞれdoubleを読み取ります。Java 2D配列。ファイル入力からの可変行と列の長さ

2 4.1 8.9

5 9.5 2.0 7.3 2.1 8.9

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

これが有するファイルI MDArray(文字列ファイル)をテストしました3 1.3 5.2 3.4

私は問題は、rowCountcolumnCountの整数は初期化されていないと思いますが、基本的な配列スキルで可変長に初期化する方法がわかりません。これは他のコンストラクタにも影響します。イントロレベルのコースなので、私はArrayListのようなより高度なテクニックを使うはずがありません。さらに、メソッドをテストする配列がないので、メソッドが正しいかどうかを検証することはできません。

編集:静的でないものやその他の変更をすべて変更するなど、回答に多くの提案を実装していましたが、私はまだNullPointerExceptionの行を取得しています私はそれがプライベートdouble[][] mdarray、割り当て仕様で必要です。今私はそれを後のメソッドでオーバーライドできるように初期化する方法を見つけることを試みています。

+2

通常の配列で実現する唯一の方法は、最初にll行を読み込み、列の最大番号を取得してから、配列を初期化してファイルを再度読み込むことです値を追加して空の値を0またはnullに設定するか – 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

あなたは、多次元の最初の2行に行と列の量を置くことによって、あなたの配列を初期化でき私は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フィールドは非静的である必要があり、それらを削除することができ、それは、ユーティリティクラスは、あなたがwouldnだった場合(それらを使用する方法でなければなりませんはるかに簡単なことが数)(

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:

「Tは、ファイルの読み込み中のアレイを作成することができます)

を任意のコンストラクタを持っています

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()では、テストは間違っています。