3
Vector v1 = new Vector();
for (int i = 0; i < 7; i++){
    v1.add(new Vector());
}
Vector WordTemp = new Vector();
for (int i = 0; i< 3; i++){
    WordTemp.add(v1);
}

Firstly I create a 3 dimensional vector. I want to put word to vector WordTemp indexed by two dimensions. Can I write the code like this?

for (int i = 0; i< 3; i++){
    for (int j = 0; j < 7; j++){
        ((Vector) ((Vector) WordTemp.get(i)).get(j)).add(word);
   }
}

After I create this vector space. can I read it like this?

...for (int i = 0; i<7; i++){
       ListIterator iter2 = ((Vector) ((Vector) WordTemp.get(t)).get(i)).listIterator();
       while(iter2.hasNext()){
           String CompareStr = (String) iter2.next();....
Musa
  • 96,336
  • 17
  • 118
  • 137
paopao87926
  • 41
  • 1
  • 1
  • 3
  • 1
    Why don't you use generic syntax? It will shave off all the ugly casting. And ArrayList is sufficient here if you are not doing any threading. – nhahtdh Jul 03 '12 at 04:29
  • Dude, I would suggest you to read chapter 7 in the SCJP 6 book.. or you can thinking of looking at Oreilly's generics and COllections in Java. You will get many ways to write the above code effectively... Joshua Bloch & Gartener has spent few precious years to simplify things for us. Let's use them. :) cheers – dharam Jul 03 '12 at 04:38
  • 1
    There is an error in the initialization code: `WordTemp.add(v1);` - this will add the same reference to `v1` 3 times in the `WordTemp` Vector, which means that any change to any of the 3 vectors will be reflected back to the others (since they are the same thing underneath). You have to initialize separate Vector containing Vector to insert into WordTemp. – nhahtdh Jul 03 '12 at 04:48
  • this will add the same reference to v1 3 times in the WordTemp Vector, which means that any change to any of the 3 vectors will be reflected back to the others (since they are the same thing underneath). Useful comment!!! – paopao87926 Jul 03 '12 at 04:59

2 Answers2

8

you can use one of these instead-

first way -

Vector<Vector<String>> s = new Vector<Vector<String>>();

second way -

Vector<String>[] s = new Vector<String>[5];

or

Vector<String>[][] s = new Vector<String>[5][5];
Kshitij
  • 8,474
  • 2
  • 26
  • 34
0

Try this,

Vector[][] s = new Vector[5][5];
Kumar Vivek Mitra
  • 33,294
  • 6
  • 48
  • 75