Are these Generics Code OK?

R

RC

Vector<String []> vector = aFunction();

String[] stringArray;
for (i = 0; i < vector.size(); i++)
stringArray = (String[])vector.get(i);



public Vector<String []> aFunction() {
Vector<String []> v = new Vector<String []>Vector();
String [] sa = new String[10];
int j = 0;
while(j < 10) {
sa[0] = "zero";
sa[1] = "one";
...
sa[9] = "nine";

v.add(sa);
j++;
}
return v;
}


Comppiled by

javac -Xlint MyProgram.java

There is no warning nor error messages.

But I got run-time error

java.lang.ClassCastException: java.lang.String

I think the error is complains about this line:

stringArray = (String[])vector.get(i);

Because that is the only line doing casting.

Any help will be appreciated!
 
O

Oliver Wong

RC said:
Vector<String []> vector = aFunction();

String[] stringArray;
for (i = 0; i < vector.size(); i++)
stringArray = (String[])vector.get(i);



public Vector<String []> aFunction() {
Vector<String []> v = new Vector<String []>Vector();
String [] sa = new String[10];
int j = 0;
while(j < 10) {
sa[0] = "zero";
sa[1] = "one";
...
sa[9] = "nine";

v.add(sa);
j++;
}
return v;
}


Comppiled by

javac -Xlint MyProgram.java

There is no warning nor error messages.

But I got run-time error

java.lang.ClassCastException: java.lang.String

I think the error is complains about this line:

stringArray = (String[])vector.get(i);

Because that is the only line doing casting.

Any help will be appreciated!


This code won't compile. Here's how you can fix it:

<code>
import java.util.Vector;

public class Test {
public static void main(String[] args) {
Vector<String[]> vector = aFunction();

String[] stringArray;
for (int i = 0; i < vector.size(); i++)
stringArray = (String[])vector.get(i);

}

public static Vector<String[]> aFunction() {
Vector<String[]> v = new Vector<String[]>();
String[] sa = new String[10];
int j = 0;
while (j < 10) {
sa[0] = "zero";
sa[1] = "one";
// ...
sa[9] = "nine";

v.add(sa);
j++;
}
return v;
}
}
</code>

this runs without error.

- Oliver
 
R

Ralf Seitner

Oliver said:
RC said:
Vector<String []> vector = aFunction();

String[] stringArray;
for (i = 0; i < vector.size(); i++)
stringArray = (String[])vector.get(i);



public Vector<String []> aFunction() {
Vector<String []> v = new Vector<String []>Vector();
String [] sa = new String[10];
int j = 0;
while(j < 10) {
sa[0] = "zero";
sa[1] = "one";
...
sa[9] = "nine";

v.add(sa);
j++;
}
return v;
}


Comppiled by

javac -Xlint MyProgram.java

There is no warning nor error messages.

But I got run-time error

java.lang.ClassCastException: java.lang.String

I think the error is complains about this line:

stringArray = (String[])vector.get(i);

Because that is the only line doing casting.

Any help will be appreciated!


This code won't compile. Here's how you can fix it:

<code>
import java.util.Vector;

public class Test {
public static void main(String[] args) {
Vector<String[]> vector = aFunction();

String[] stringArray;
for (int i = 0; i < vector.size(); i++)
stringArray = (String[])vector.get(i);

}

public static Vector<String[]> aFunction() {
Vector<String[]> v = new Vector<String[]>();
String[] sa = new String[10];
int j = 0;
while (j < 10) {
sa[0] = "zero";
sa[1] = "one";
// ...
sa[9] = "nine";

v.add(sa);
j++;
}
return v;
}
}
</code>

this runs without error.

- Oliver
Hi!
OK. I just wanted to send an answer, but Oliver was quicker than me...
Just one thing, I want to add. You do not have to cast explicitly to
String[], because your vector only contains String[], due to the
declaration Vector<String[]>vector.
bye, Ralf
 
S

Steve W. Jackson

RC said:
Vector<String []> vector = aFunction();

String[] stringArray;
for (i = 0; i < vector.size(); i++)
stringArray = (String[])vector.get(i);

Forgetting for the moment that the above code is without context...not
in any class, method, etc...you could make some improvements to the
loop. One thing is that there's no need to cast your "vector.get" to a
String[] since it was explicitly created to contain only such. But then
you can use the improved for loop that comes in 1.5, like this:

for (String[] stringArray : vector) {
// do something here with the current stringArray
}
public Vector<String []> aFunction() {
Vector<String []> v = new Vector<String []>Vector();
String [] sa = new String[10];
int j = 0;
while(j < 10) {
sa[0] = "zero";
sa[1] = "one";
...
sa[9] = "nine";

v.add(sa);
j++;
}
return v;
}


Comppiled by

javac -Xlint MyProgram.java

There is no warning nor error messages.

But I got run-time error

java.lang.ClassCastException: java.lang.String

I think the error is complains about this line:

stringArray = (String[])vector.get(i);

Because that is the only line doing casting.

Any help will be appreciated!

That last part is a little unclear...how could it compile? What is new
"Vector<String []>Vector();" supposed to mean?

And a ClassCastException should be accompanied by a stack trace that
could show you the exact line where it originates. But since the above
is fragmented and cannot be compiled as presented, it's not clear what
your problem might be.
 
R

RC

Steve said:
That last part is a little unclear...how could it compile? What is new
"Vector<String []>Vector();" supposed to mean?


Sorry, that is typo error. It should be


Vector<String []> v = new Vector<String []>();
 
C

Chris Smith

RC said:
Steve said:
That last part is a little unclear...how could it compile? What is new
"Vector<String []>Vector();" supposed to mean?

Sorry, that is typo error. It should be

Vector<String []> v = new Vector<String []>();

Just out of curiosity, did you actually retype all of your code into
your news client? If so, wow. That is not only bad for us, since
you're bound to produce typos, but it must also be a real pain for you
as well! Copy and paste really is much easier.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top