2013-02-20 356 views
-6
package testing; 

import java.util.Random; 
import java.util.Vector; 

public class Variable2Variable { 


    public static void main(String[] args) { 
     Vector<Vector<Integer>> lines; 
     Vector<Integer> linePoints; 
     Random rndNumbers = new Random(); 

     lines = new Vector<Vector<Integer>>(); 
     linePoints = new Vector<Integer>(); 

     for(int i = 0; i < 5; i++){ 
      linePoints.add(rndNumbers.nextInt(10)); 
      linePoints.add(rndNumbers.nextInt(10)); 
     } 

     lines.add(linePoints); 

     //linePoints.clear(); 

     System.out.println("Vector: lines: " + lines); 
     System.out.println("Vector: linePoints: " + linePoints); 
    } 

} 

請運行上面的代碼,記下結果,然後取消註釋行「//linePoints.clear();」然後再運行一次。將元素從一個向量移動到另一個向量

我怎樣才能從矢量'linePoints'中移動元素並將它們存儲到矢量'線'中? 謝謝。

+5

號我拒絕任何運行。我們大多數人都會這樣做。把結果,以及預期的結果放在這裏......順便說一句,你爲什麼要清除'linePoints'?您只是將該列表添加到「完整列表列表」對象的行中。如果你清理它,因爲這是_same instance_作爲你放入'lines'的那個,它將是空的。 – ppeterka 2013-02-20 14:02:03

回答

3

而不是

lines.add(linePoints); 

    //linePoints.clear(); 

    System.out.println("Vector: lines: " + lines); 
    System.out.println("Vector: linePoints: " + linePoints); 

這樣來做:

lines.add(linePoints); 

    linePoints = new Vector<Integer>(); 

    System.out.println("Vector: lines: " + lines); 
    System.out.println("Vector: linePoints: " + linePoints); 

也有一些注意事項:

  • 你正在做的OOP都錯了。您可以將非結構化的Integers列表語義上用作Integers對的列表。這爲自己的班級尖叫!
  • 爲什麼使用向量,當你可以使用ArrayLists呢?你需要他們的線程安全的性質?

TL; DR部分

你寫的代碼的說明:

lines = new Vector<Vector<Integer>>(); //create list instance holding list of Integers 
    linePoints = new Vector<Integer>(); //create list instance holding list of Integers 

    for(int i = 0; i < 5; i++){ // fill list of Integers with even number of Integers 
     linePoints.add(rndNumbers.nextInt(10)); 
     linePoints.add(rndNumbers.nextInt(10)); 
    } 

    lines.add(linePoints); 
    //add a reference pointing at the "list of Integers **instance**" 
    //to the list of list of Integers instance 
    //No copying happens here! Still only two lists, one has a reference to the other. 
    //to copy, you should do: 
    //lines.add(new Vector<Integer>(linePoints)); 

    linePoints.clear(); //blam! this removes the integers from the list of Integers 
    // as linePoints has only a reference to the list instance itself, 
    // that has now the same empty list... 

    System.out.println("Vector: lines: " + lines); //empty 
    System.out.println("Vector: linePoints: " + linePoints); //1 element, the empty list 

的修復是如何工作的說明:

lines = new Vector<Vector<Integer>>(); //create list instance holding list of Integers 
    linePoints = new Vector<Integer>(); //create list instance holding list of Integers 

    for(int i = 0; i < 5; i++){ // fill list of Integers with even number of Integers 
     linePoints.add(rndNumbers.nextInt(10)); 
     linePoints.add(rndNumbers.nextInt(10)); 
    } 

    lines.add(linePoints); 
    //add a reference pointing at the "list of Integers **instance**" 
    //to the list of list of Integers instance 

    linePoints = new Vector<Integer>(); //create new empty list, 
    //and set the reference linePoints to point to that list. 
    //the original instance is left alone, lines list's reference still points to it 

    //there are now 3 instances of List in memory now: 
    // * new empty Vector<Integer>, linePoints references it 
    // * the Vector<Vector<Integer>> instance, lines references it 
    // * the "old" Vector<Integer> instance, the first element of lines references it 

    System.out.println("Vector: lines: " + lines); //empty 
    System.out.println("Vector: linePoints: " + linePoints); //1 element: the list, having the integers 
+0

謝謝ppeterka ....使用「linePoints = new Vector ();」工作得很好,因此取代了「linePoints.clear();」 – user2091486 2013-02-21 09:16:17

相關問題