2013-01-23 24 views
-1

我試圖使用從一個文本文件中的信息存儲到一個數組,但該項目要求我們通過進行臨時數組,並提高其大小當我們讀更多的文件器。---新的對象這樣做。我怎樣才能做到這一點?如何製作一個更大的數組並複製java中前一個數組的信息?

/** 
* reads a csv data file and returns an array of acquaintances 
* @param path File path of CSV file 
* @return Acquaintances from the file 
*/ 
public static Acquaintance[] read (String path) { 
    //create an array of acquaintances 
    Acquaintance[] acqs = new Acquaintance[0]; 

    //open the file 
    try { 
     BufferedReader file = new BufferedReader(new FileReader(path)); 

     //read the file until the end 
     String line; 

     while((line = file.readLine()) != null) { 
      // parse the line just read 
      String[] parts = line.split(","); 

      //create an acquaintance object 
      Acquaintance a = new Acquaintance(parts[0], parts[1], Double.parseDouble(parts[2])); 

      acqs[0] = a; 

      //Add the object to the array 

      //(1) create a new Acquaintance array, with one extra element 
      Acquaintance[] tmp = new Acquaintance[acqs.length+1]; 

      //(2) copy all old elements into new 
      Acquaintance[] tmp = acqs.clone(); 

      //(3) assign new Acquaintance object to last element of the array 

      //(4) assign new array's address to acqs 
      //for loop 
     } 
+1

這似乎可能是作業? –

+0

檢查'ArrayList'的'ensureCapaciy'方法:http://grepcode.com/file/repository.grepcode.com/java/root/jdk/openjdk/6-b14/java/util/ArrayList.java#ArrayList。 ensureCapacity%28int%29 –

+1

不要在有人給出答案後刪除問題。這使其他人無用。 –

回答

0

使用

Arrays.copyOfRange(T[] original,int from, int to) 

方法來複制一個array到不同勢array

+0

老師說要用for循環。我怎樣才能把它組合成比以前更大的數組1,然後將舊數組傳遞給新數組,然後將新數組指定爲舊數組? – user2002484

+1

'copyOfRange'方法只能使數組變小或保持其大小。它不能使數組更大,因爲問題需要。 –

3

如果你的老師要求你應該在課堂上更緊密地聽取:-)

的具體方法你可以像這樣增加一個數組:

myArray = Arrays.copyOf(myArray,myArray.length+1); 

但是沒有可以看到的循環。您可以使用這樣舊的Java方法做到這一點:

Object[] tmpArray = new Object[myArray.length+1]; 
System.arraycopy(myArray,0,tmpArray,0,myArray.length); 
myArray = tmpArray; 

同樣,沒有for循環必需的。唯一的好處是它可以與Java 1.5一起運行。在這個問題中for循環唯一的「需要」是做System.arraycopy(它被Arrays.copyOf使用)的效率更高。就像這樣:

Object[] tmpArray = new Object[myArray.length+1]; 
for(int i=0;i<myArray.length;i++) tmpArray[i]=myArray[i]; 
myArray = tmpArray; 

的想法是這樣,讓您在使用for循環,而不是實際的一個好辦法解決這個問題練習。

增長,因爲你使用它的一個陣列最簡單的方法是使用某種形式的Listjava.util.ArrayList例如,讓列表手柄存儲數據,而當你完成調用列表的toArray()方法。

0

我要去忽視的事實是要求使用數組這無論如何是沒有意義的。你想要做的是給你的初始數組指定一個給定的大小,並且每當你到達存儲空間的末尾時增加一個設定的數量。通常情況下,每當空間不足時,您都會將底層存儲擴大兩倍。繼承人一些僞代碼:

public Acquaintance[] getAcquaitances(final String path) { 
    if(path == null) return null; 

    int count = 0, initialSize = 16; 
    Acquaintance[] temporaryArr = new Acquaintance[initialSize]; 

    // Open file here 

    String line = null; 
    while((line = file.readLine()) != null) { 
     if(count >= temporaryArr.length) { 
      Acquaintance[] switch = temporaryArr; 
      temporaryArr = new Acquaintance[switch.length * 2] // increase size by factor of 2 
      for(int i = 0; i < switch.length; i++) { 
       temporaryArr[i] = switch[i]; 
      } 
     } 

     temporaryArr[count] = createAcquantainceFromLine(line); 

     ++count; 
    } 

    Acquaintance[] results = new Acquaintance[count]; 
    for(int i = 0; i < count; i++) { 
     results[i] = temporaryArr[i]; 
    } 

    return results; 
} 

稍作修改,上面的代碼應該適合你。研究它,嵌入它,但然後完全忽略它,如果你有未來擴展陣列的需求,請使用ArrayList來代替。或者System.arraycopy

相關問題