2010-10-18 96 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