Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Are these Generics Code OK?

Reply
Thread Tools

Are these Generics Code OK?

 
 
RC
Guest
Posts: n/a
 
      08-30-2006


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!
 
Reply With Quote
 
 
 
 
Oliver Wong
Guest
Posts: n/a
 
      08-30-2006

"RC" <(E-Mail Removed)> wrote in message
news:ed4g9g$qbl$(E-Mail Removed)...
>
>
> 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

 
Reply With Quote
 
 
 
 
Ralf Seitner
Guest
Posts: n/a
 
      08-30-2006
Oliver Wong schrieb:
>
> "RC" <(E-Mail Removed)> wrote in message
> news:ed4g9g$qbl$(E-Mail Removed)...
>>
>>
>> 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
 
Reply With Quote
 
Steve W. Jackson
Guest
Posts: n/a
 
      08-30-2006
In article <ed4g9g$qbl$(E-Mail Removed)>,
RC <(E-Mail Removed)> wrote:

> 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.
--
Steve W. Jackson
Montgomery, Alabama
 
Reply With Quote
 
RC
Guest
Posts: n/a
 
      08-30-2006
Steve W. Jackson wrote:

>
> 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 []>();
 
Reply With Quote
 
Chris Smith
Guest
Posts: n/a
 
      08-31-2006
RC <(E-Mail Removed)> wrote:
> Steve W. Jackson wrote:
> >
> > 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.

--
Chris Smith
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
generics depending on generics Soul VHDL 0 02-02-2009 09:14 AM
Any tool to convert java raw code (a la java 1.4) into generics code Royan Java 8 02-15-2008 02:35 PM
Generics vs Constants - what criteria do you use to choose between these? Andrew FPGA VHDL 19 10-11-2006 02:20 AM
Can't convert a generics list of objects into a generics list ofinterfaces Juergen Berchtel Java 1 05-20-2005 02:07 PM
Deprecated functionality with generics (previously valid code now invalid) mortoray@ecircle-ag.com Java 2 01-18-2005 08:18 PM



Advertisments