Java:调整多维数组的大小

4
我有一个由字符串构建的多维数组,最初大小为[50][50],这太大了,现在数组充满了null值,我正在尝试删除这些空值,我已经成功将数组大小调整为[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

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

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

我也明白在这里使用列表会是更好的选择,但由于外部限制,这是不可能的。它只能是多维数组。


如果不能使用列表,您可以自己实现动态数组的实现,在需要时扩展数组并将旧值复制到新构建的数组中。或者您可以两次读取文件。首先找到行数和列数,然后在第二次读取中定义数组,在数组中填入值。 - Gaurav Saxena
3个回答

3

我认为你的程序需要三个修改

  • 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]]

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

谢谢,这正是我想要做的,我发誓我尝试过了,当时已经很晚了,可能犯了错误,多次尝试同样的事情。非常感谢,我会点赞你的回答,但我没有足够的声望。 - Aaron
@Aaron,很高兴能帮到你。顺便说一句,无论你的声望有多少,你都可以为自己的问题投票。 - mikej

2

我猜这是一项作业任务,但看起来你已经对它进行了一些思考。

不要创建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

0

这里有一个观察和建议。

观察:在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] /*, ... */));
}

我本来会这样做,但是我不能保证CSV文件每次都使用相同的标题行,那只是一个正确格式的示例CSV,我永远不知道数据实际上可能包含什么。 - Aaron

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接