-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' から要素を移動し、それらをベクター 'lines' に格納するにはどうすればよいですか? ありがとう。

4

1 に答える 1

3

Instead of

    lines.add(linePoints);

    //linePoints.clear();

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

Do it this way:

    lines.add(linePoints);

    linePoints = new Vector<Integer>();

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

Also some notes:

  • you are doing OOP all wrong. You use an unstructured list of Integers semantically as list of pairs of Integers. This screams for its own class!
  • why do you use Vectors, when you could use ArrayLists too? Do you need their thread safe nature?

TL;DR part

Explanation of the code you wrote:

    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

Explanation of how the fix works:

    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
于 2013-02-20T14:07:16.470 に答える