2016-12-06 62 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)