Are these Generics Code OK?

Discussion in 'Java' started by RC, Aug 30, 2006.

  1. RC

    RC Guest

    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!
     
    RC, Aug 30, 2006
    #1
    1. Advertising

  2. RC

    Oliver Wong Guest

    "RC" <> wrote in message
    news:ed4g9g$qbl$...
    >
    >
    > 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
     
    Oliver Wong, Aug 30, 2006
    #2
    1. Advertising

  3. RC

    Ralf Seitner Guest

    Oliver Wong schrieb:
    >
    > "RC" <> wrote in message
    > news:ed4g9g$qbl$...
    >>
    >>
    >> 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
     
    Ralf Seitner, Aug 30, 2006
    #3
  4. In article <ed4g9g$qbl$>,
    RC <> 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
     
    Steve W. Jackson, Aug 30, 2006
    #4
  5. RC

    RC Guest

    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 []>();
     
    RC, Aug 30, 2006
    #5
  6. RC

    Chris Smith Guest

    RC <> 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
     
    Chris Smith, Aug 31, 2006
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Mike Mike

    How to compile these code by gcj?

    Mike Mike, Jun 2, 2004, in forum: Java
    Replies:
    1
    Views:
    508
  2. Juergen Berchtel
    Replies:
    1
    Views:
    6,060
    John C. Bollinger
    May 20, 2005
  3. Andrew FPGA
    Replies:
    19
    Views:
    909
    Jim Lewis
    Oct 11, 2006
  4. Royan
    Replies:
    8
    Views:
    766
    Patricia Shanahan
    Feb 15, 2008
  5. Soul
    Replies:
    0
    Views:
    539
Loading...

Share This Page