Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / First Aid / August 2006

Tip: Looking for answers? Try searching our database.

Are these Generics Code OK?

Thread view: 
RC - 30 Aug 2006 18:01 GMT
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!
Oliver Wong - 30 Aug 2006 18:35 GMT
> Vector<String []> vector = aFunction();
>
[quoted text clipped - 35 lines]
>
> 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
Ralf Seitner - 30 Aug 2006 18:44 GMT
Oliver Wong schrieb:

>> Vector<String []> vector = aFunction();
>>
[quoted text clipped - 72 lines]
>
>    - 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
Steve W. Jackson - 30 Aug 2006 18:46 GMT
> 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();
[quoted text clipped - 29 lines]
>
> 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.
Signature

Steve W. Jackson
Montgomery, Alabama

RC - 30 Aug 2006 19:56 GMT
> 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 []>();
Chris Smith - 31 Aug 2006 05:14 GMT
> > 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.

Signature

Chris Smith



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.