[1.4] argv[] doesn't understand size() and iterator()??

Discussion in 'Java' started by =?ISO-8859-1?Q?Thomas_Gagn=E9?=, Jul 2, 2003.

  1. I downloaded 1.4 yesterday, and tried compiling two very simple programs. I'm
    re-exploring Java after three years and thought the following programs
    should work.

    tgagne:/home/tgagne/work/java which javac
    /opt/j2sdk_nb/j2sdk1.4.2/bin/javac
    tgagne:/home/tgagne/work/java cat echo1.java
    import java.util.*;

    public class echo1 {
    public static void main(String argv[]) {
    int i;

    for (i = 0; i < argv.size(); i++)
    System.out.println(argv);
    }
    }
    tgagne:/home/tgagne/work/java javac echo1.java
    echo1.java:7: cannot resolve symbol
    symbol : method size ()
    location: class java.lang.String[]
    for (i = 0; i < argv.size(); i++)
    ^
    1 error
    tgagne:/home/tgagne/work/java cat echo2.java
    import java.util.*;

    public class echo2 {
    public static void main(String argv[]) {
    Iterator i;

    for (i = argv.iterator(); i.hasNext(); )
    System.out.println(i.next());
    }
    }
    tgagne:/home/tgagne/work/java javac echo2.java
    echo2.java:7: cannot resolve symbol
    symbol : method iterator ()
    location: class java.lang.String[]
    for (i = argv.iterator(); i.hasNext(); )
    ^
    1 error
    --
    ..tom
    remove dashes in email for replies
    http://isectd.sourceforge.net
    =?ISO-8859-1?Q?Thomas_Gagn=E9?=, Jul 2, 2003
    #1
    1. Advertising

  2. Thomas Gagné:

    >I downloaded 1.4 yesterday, and tried compiling two very simple programs. I'm
    > re-exploring Java after three years and thought the following programs
    >should work.


    It seems that you are confusing arrays with lists (types implementing
    java.util.List).

    Arrays have a length field, not a size method. So use "i <
    argv.length".

    Besides, arrays don't have an iterator() method, that's also a List
    feature. Obviously one could write an Iterator that works on arrays.

    Regards,
    Marco
    --
    Please reply in the newsgroup, not by email!
    Java programming tips: http://jiu.sourceforge.net/javatips.html
    Other Java pages: http://www.geocities.com/marcoschmidt.geo/java.html
    Marco Schmidt, Jul 2, 2003
    #2
    1. Advertising

  3. Thanks. That worked.

    I tried "argv.asList()" with an error--which I didn't expect.

    I would also have expected that instead of
    Arrays.asList(argv)
    I could have used
    argv.asList()
    but that wasn't understood. I guess I have to get used to tripping over these
    little things until fully submerged in the Java way.

    Thank you!


    --
    ..tom
    remove dashes in email for replies
    http://isectd.sourceforge.net
    =?ISO-8859-1?Q?Thomas_Gagn=E9?=, Jul 2, 2003
    #3
  4. =?ISO-8859-1?Q?Thomas_Gagn=E9?=

    Ian McCall Guest

    "Thomas Gagné" <> wrote in message
    news:...
    > I would also have expected that instead of
    > Arrays.asList(argv)
    > I could have used
    > argv.asList()
    > but that wasn't understood. I guess I have to get used to tripping over

    these
    > little things until fully submerged in the Java way.


    The difference is that the array construct is essentially a primitive, not
    an object as such. Because it's not an object, you can't call methods on it.

    The closest equivalent object would be the ArrayList, and there are also
    utility returns to handle arrays inside the Arrays class.


    Hope this helps,
    Ian
    Ian McCall, Jul 2, 2003
    #4
  5. In article <uvDMa.1856$>, Ian McCall wrote:
    > The difference is that the array construct is essentially a primitive, not
    > an object as such. Because it's not an object, you can't call methods on it.


    According the the Java Language Specification an array is an object and you
    can invoke the methods of class Object on an array. Here is a quote from
    http://java.sun.com/docs/books/jls/second_edition/html/arrays.doc.html#27805

    In the Java programming language arrays are objects (§4.3.1), are
    dynamically created, and may be assigned to variables of type Object
    (§4.3.2). All methods of class Object may be invoked on an array.
    A. Bolmarcich, Jul 2, 2003
    #5
  6. =?ISO-8859-1?Q?Thomas_Gagn=E9?=

    Ian McCall Guest

    "A. Bolmarcich" <9.net> wrote in message
    news:9.net...
    > According the the Java Language Specification an array is an object...


    Yeah, that's why I said 'essentially' a primitive. It's an odd area, arrays,
    and I find them quite inconsistent with the rest of the language. They
    aren't treated in anything like the same way as any other form of
    collection.


    Cheers,
    Ian
    Ian McCall, Jul 2, 2003
    #6
  7. Thomas Gagné wrote:
    > In that case the only confusion is the interface to Array is
    > (unnecessarily?) different than ArrayList. Both are collections of
    > components of a common type. It is confusing to beginners why they
    > would be treated differently.
    >
    > I'm sure this horse has been bludgeoned to death, but it would be nice
    > if Java's collections all descended from a common collection class.


    They do: Collection. But arrays are not collections in that sense.
    Java arrays are a built-in language feature that is exposed in Object
    form. Some of the collection classes are built on top of arrays, but
    the collection classes are not inherent language features, they are part
    of the platform API.

    The collection classes are fundamentally different from arrays, by the
    way, with regard to component types. Arrays can be constructed specific
    to components of any Java primitive or reference type, but the standard
    collections hold only Objects (no primitives, and no enforcement of any
    more specific typing). Also, arrays are fixed-size, whereas (most)
    collections are flexible-size. Collections and arrays have similar
    roles, but they are more different than you seem to think.

    I venture to suggest that Java arrays make abundant sense to most people
    coming to Java from common lower-level programming languages, especially
    C and C++, but also Fortran, Pascal, and others. Most of those are
    non-OO languages, of course.

    I will furthermore suggest that you learn the basic language features
    first before plunging into the platform API. Sun's Java Tutorial is
    good, as is "Thinking in Java". There are other good introductions to
    the language as well.


    Regards,

    John Bollinger
    John C. Bollinger, Jul 2, 2003
    #7
  8. What do you mean by "platform API"?

    John C. Bollinger wrote:
    > <snip>
    >
    > They do: Collection. But arrays are not collections in that sense. Java
    > arrays are a built-in language feature that is exposed in Object form.
    > Some of the collection classes are built on top of arrays, but the
    > collection classes are not inherent language features, they are part of
    > the platform API.
    >
    > <snip>
    >
    > I will furthermore suggest that you learn the basic language features
    > first before plunging into the platform API. Sun's Java Tutorial is
    > good, as is "Thinking in Java". There are other good introductions to
    > the language as well.
    >


    I'm trying, starting with Thinking in Java, but am perhaps handicapped at
    approaching Java from an OO background rather than a procedural one.


    --
    ..tom
    remove dashes in email for replies
    http://isectd.sourceforge.net
    =?ISO-8859-1?Q?Thomas_Gagn=E9?=, Jul 3, 2003
    #8
  9. =?ISO-8859-1?Q?Thomas_Gagn=E9?=

    Jon Skeet Guest

    Ian McCall <> wrote:
    >
    > "A. Bolmarcich" <9.net> wrote in message
    > news:9.net...
    > > According the the Java Language Specification an array is an object...

    >
    > Yeah, that's why I said 'essentially' a primitive. It's an odd area, arrays,
    > and I find them quite inconsistent with the rest of the language. They
    > aren't treated in anything like the same way as any other form of
    > collection.


    No, but they're treated exactly the same as any other object. They're
    far more "object-like" than primitive-like.

    --
    Jon Skeet - <>
    http://www.pobox.com/~skeet/
    If replying to the group, please do not mail me too
    Jon Skeet, Jul 3, 2003
    #9
  10. =?ISO-8859-1?Q?Thomas_Gagn=E9?=

    Ian McCall Guest

    "Jon Skeet" <> wrote in message
    news:...
    > Ian McCall <> wrote:
    > > ...It's an odd area, arrays,
    > > and I find them quite inconsistent with the rest of the language. They
    > > aren't treated in anything like the same way as any other form of
    > > collection.

    >
    > No, but they're treated exactly the same as any other object. They're
    > far more "object-like" than primitive-like.


    Hmm. I'm going to plead poor phrasing here, but I still believe that arrays
    in Java are quite odd. My description as 'essentially primitive' was poorly
    chosen, but Java arrays still behave more like C arrays (or more accurately,
    like Ada arrays with their attributes) rather than exhibiting object-like
    behaviour.

    I fully accept that you're right within the language spec, but in
    actual -usage- an array feels like a hack into the language design. If they
    truly behaved in an object-like fashion, then why are there so many static
    utility classes to deal with them (Array, ArrayUtils, converting to
    ArrayLists etc.) rather than methods off the object itself?. I meant my
    description to reflect the fact that trying to use collection-style routines
    on them is pretty meaningless, even though in reality they -are-
    collections.

    Am I coming across a bit better? Or is my foot still in my mouth...?



    Cheers,
    Ian
    Ian McCall, Jul 3, 2003
    #10
  11. Ian McCall wrote:
    <snip>
    >
    > Am I coming across a bit better? Or is my foot still in my mouth...?
    >
    >


    Reads fine.



    --
    ..tom
    remove dashes in email for replies
    http://isectd.sourceforge.net
    =?ISO-8859-1?Q?Thomas_Gagn=E9?=, Jul 3, 2003
    #11
  12. =?ISO-8859-1?Q?Thomas_Gagn=E9?=

    Jon Skeet Guest

    Ian McCall <> wrote:
    > Hmm. I'm going to plead poor phrasing here, but I still believe that arrays
    > in Java are quite odd. My description as 'essentially primitive' was poorly
    > chosen, but Java arrays still behave more like C arrays (or more accurately,
    > like Ada arrays with their attributes) rather than exhibiting object-like
    > behaviour.
    >
    > I fully accept that you're right within the language spec, but in
    > actual -usage- an array feels like a hack into the language design. If they
    > truly behaved in an object-like fashion, then why are there so many static
    > utility classes to deal with them (Array, ArrayUtils, converting to
    > ArrayLists etc.) rather than methods off the object itself?.


    Because the Array class came to the party late (1.2), and it was
    probably seen as too late to start adding methods to arrays themselves
    - that would have involved a JLS and JVM spec change.

    > I meant my
    > description to reflect the fact that trying to use collection-style routines
    > on them is pretty meaningless, even though in reality they -are-
    > collections.
    >
    > Am I coming across a bit better? Or is my foot still in my mouth...?


    The above is certainly accurate, yes :)

    --
    Jon Skeet - <>
    http://www.pobox.com/~skeet/
    If replying to the group, please do not mail me too
    Jon Skeet, Jul 3, 2003
    #12
  13. "Ian McCall" <> wrote in message news:<rdTMa.1545$>...

    > I fully accept that you're right within the language spec, but in
    > actual -usage- an array feels like a hack into the language design. If they
    > truly behaved in an object-like fashion, then why are there so many static
    > utility classes to deal with them (Array, ArrayUtils, converting to
    > ArrayLists etc.) rather than methods off the object itself?.


    Well, it's a good idea to have _some_ kind of access to arrays or
    array-like structures in a language, or a great many programming
    techniques will have to be rethought. The designers of Java decided
    to expose array functionality through a thin Object wrapper, so that
    programmers could pass 'em around by reference without having to roll
    their own wrapper class. It's not a perfect fit, but if you really,
    REALLY want pure OO, go talk to the Smalltalk folks. And, one more
    thing: ArrayUtils? Where's this class defined? Is it a Java 1.5
    thing?
    Jonathan Oexner, Jul 3, 2003
    #13
  14. Jonathan Oexner wrote:
    >
    > Well, it's a good idea to have _some_ kind of access to arrays or
    > array-like structures in a language, or a great many programming
    > techniques will have to be rethought.


    I missed the spot where anyone advocated against the existence of arrays. On
    the contrary, it was only pointed out that they should have been a subclass of
    a collection class.

    > The designers of Java decided
    > to expose array functionality through a thin Object wrapper, so that
    > programmers could pass 'em around by reference without having to roll
    > their own wrapper class.


    Objects are already passed around by reference. What would require a wrapper?

    > It's not a perfect fit, but if you really,
    > REALLY want pure OO, go talk to the Smalltalk folks.


    It's too bad more people haven't.

    > And, one more
    > thing: ArrayUtils? Where's this class defined? Is it a Java 1.5
    > thing?



    --
    ..tom
    remove dashes in email for replies
    http://isectd.sourceforge.net
    =?ISO-8859-1?Q?Thomas_Gagn=E9?=, Jul 3, 2003
    #14
    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. Bret

    char **argv vs. char* argv[]

    Bret, Aug 31, 2003, in forum: C Programming
    Replies:
    21
    Views:
    4,550
    Richard Heathfield
    Sep 3, 2003
  2. David
    Replies:
    10
    Views:
    5,913
    Richard Heathfield
    Sep 15, 2003
  3. Hal Styli
    Replies:
    14
    Views:
    1,609
    Old Wolf
    Jan 20, 2004
  4. =?ISO-8859-1?Q?Thomas_N=FCcker?=

    sys.argv[0] - 'module' object has no attribute 'argv'

    =?ISO-8859-1?Q?Thomas_N=FCcker?=, Jun 30, 2003, in forum: Python
    Replies:
    0
    Views:
    883
    =?ISO-8859-1?Q?Thomas_N=FCcker?=
    Jun 30, 2003
  5. jab3

    char **argv & char *argv[]

    jab3, Dec 4, 2004, in forum: C Programming
    Replies:
    5
    Views:
    648
    Chris Torek
    Dec 8, 2004
Loading...

Share This Page