繁体   English   中英

Java:调整多维数组的大小

[英]Java : Resizing a multidimensional array

我有一个由Strings构建的多维数组,最初用大小[50] [50]创建,这太大了,现在数组已经满了空值,我正在尝试删除这些所说的空值,我已经管理过将数组的大小调整为[requiredSize] [50],但不能再缩小它,任何人都可以帮我解决这个问题吗? 我已经在互联网上搜索了这样的答案,但找不到它。

这也是我的完整代码(我意识到我的代码中可能有一些非常不干净的部分,我还没有清理任何东西)

import java.io.*;
import java.util.*;

public class FooBar
{
  public static String[][] loadCSV()
  {
    FileInputStream inStream;
    InputStreamReader inFile;
    BufferedReader br;
    String line;
    int lineNum, tokNum, ii, jj;
    String [][] CSV, TempArray, TempArray2;

    lineNum = tokNum = ii = jj  = 0;
    TempArray = new String[50][50];

    try
    {
        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
        System.out.println("Please enter the file path of the CSV");
        String fileName = in.readLine();
        inStream = new FileInputStream(fileName);
        inFile = new InputStreamReader(inStream);
        br = new BufferedReader(inFile);
        StringTokenizer tok,tok2;


        lineNum = 0;
            line = br.readLine();
            tokNum = 0;
            tok = new StringTokenizer(line, ",");

            while( tok.hasMoreTokens())
            {
                TempArray[tokNum][0] = tok.nextToken();
                tokNum++;
            }
            tokNum = 0;
            lineNum++;

            while( line != null)
            {
                    line = br.readLine();
                    if (line != null)
                    {
                        tokNum = 0;
                        tok2 = new StringTokenizer(line, ",");

                        while(tok2.hasMoreTokens())
                        {
                            TempArray[tokNum][lineNum] = tok2.nextToken();
                            tokNum++;
                        }
                    }
                    lineNum++;
            }
    }    
    catch(IOException e)
    {
        System.out.println("Error file  may not be accessible, check the path and try again");
    }

    CSV = new String[tokNum][50];
    for (ii=0; ii<tokNum-1 ;ii++)
    {
        System.arraycopy(TempArray[ii],0,CSV[ii],0,TempArray[ii].length);
    }
    return CSV;
}    
public static void main (String args[])
{
    String [][] CSV;
    CSV = loadCSV();
    System.out.println(Arrays.deepToString(CSV));
}
}                

CSV文件如下所示

Height,Weight,Age,TER,Salary
163.9,46.8,37,72.6,53010.68
191.3,91.4,32,92.2,66068.51
166.5,51.1,27,77.6,42724.34
156.3,55.7,21,81.1,50531.91

它可以采用任何大小,但这只是一个示例文件。

我只需要调整数组的大小,使其不包含任何空值。

我也理解列表在这里是一个更好的选择,但由于外部限制,它是不可能的。 它只能是一个多维数组。

我认为你需要对你的程序进行3次更改

  • while循环之后while lineNum将比文件中的行数多1,因此不是将CSV声明为String[tokNum][50]而是将其声明为CSV = new String[tokNum][lineNum-1];
  • tokNum将是一行中的字段数,因此你的for循环条件应该是ii<tokNum而不是ii<tokNum-1
  • arraycopy的最后一个参数应该是lineNum-1

即用于构建CSV阵列的修改代码是:

CSV = new String[tokNum][lineNum-1];
for (ii=0; ii<tokNum ;ii++)
{
    System.arraycopy(TempArray[ii],0,CSV[ii],0,lineNum-1);
}

然后输出将是:

[[Height, 163.9, 191.3, 166.5, 156.3], [Weight, 46.8, 91.4, 51.1, 55.7], 
[Age, 37, 32, 27, 21], [TER, 72.6, 92.2, 77.6, 81.1],
[Salary, 53010.68, 66068.51, 42724.34, 50531.91]]

请注意,您实际上不需要与其他文件分开处理文件的第一行,但这是您可以在清理过程中涵盖的内容。

10到1这是一个家庭作业。 但是,看起来你已经考虑过了。

不要制作TempArray变量。 制作一个“字符串列表”。 就像是:

List<List<String>> rows = new ArrayList<ArrayList<String>>();

while(file.hasMoreRows()) {            //not valid syntax...but you get the jist
  String rowIText = file.nextRow();    //not valid syntax...but you get the jist
  List<String> rowI = new ArrayList<String>(); 

  //parse rowIText to build rowI --> this is your homework

  rows.add(rowI);

}

//now build String[][] using fully constructed rows variable

这是一个观察和建议。

观察:在Java中使用(多维)数组很困难。

建议:不要使用数组来表示Java中的复杂数据类型。

为您的数据创建类。 创建人员列表:

class Person {
    String height;  //should eventually be changed to a double probably
    String weight;  // "
    //...

    public Person( String height, String weight /*, ... */ ) {
       this.height = height;
       this.weight = weight;
       //...
    }
}

List<Person> people = new ArrayList<Person>();
String line;
while ( (line = reader.nextLine()) != null ) {
    String[] records = line.split(",");
    people.add(new Person (records[0], records[1] /*, ... */));
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM