2010-10-18 170 views
4

我有一个从字符串构建的多维数组,最初是用大小[50] [50]创建的,这个数组太大了,现在数组已满了空值,我目前正在尝试删除这些空值,我已经设法将数组的大小调整为[requiredSize] [50],但无法进一步缩小,任何人都可以帮助我吗?我已经在互联网上搜索了这样一个答案,但找不到它。Java:调整多维数组的大小

这里是我完整的代码太(我知道有可能是在我的代码一些非常不干净的部分,我还没有清理任何东西)

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 

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

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

我也明白一个列表将是一个更好的选择,但这是不可能的,由于外部约束。它只能是一个多维数组。

+0

如果使用列表是不是一种选择,你可以有你自己的动态数组实现,你的阵列扩大,因为和需要和新建的数组中复制旧的值时。或者你可以读两次文件。首先找到行数和列数,然后在第二次读取来定义数组,填入数组中的值 – 2010-10-18 18:24:43

回答

2

我想你需要3名更改程序

  • while循环lineNum后会比文件中的行数1更是这样,而不是宣布CSV到​​3210声明为CSV = new String[tokNum][lineNum-1];
  • tokNum将是连续的字段数,因此您的for循环条件应为ii<tokNum而不是ii<tokNum-1
  • 的最后一个参数应该是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]] 

注意,你并不真的需要将文件的第一行与其他文件分开处理,但这是作为清理的一部分可以涵盖的内容。

+0

感谢这正是我想要做的,我发誓,这是我试过的,这是深夜,我可能会犯了一个错误,并多次尝试同样的事情。谢谢你,我会按你的答案向上箭头,但我没有所需的声望 – Aaron 2010-10-19 03:33:03

+0

@Aaron很高兴它帮助。 PS。无论您拥有多少声望,您都可以为自己的*自己的问题提供答案。 – mikej 2010-10-19 07:08:59

2

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 
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] /*, ... */)); 
} 
+0

但是我会这样做的,我不能保证每次都会使用这些相同的头文件,这只是一个带有正确格式的CSV样本,我永远不知道数据实际上可以保存什么 – Aaron 2010-10-19 03:30:56