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

  • Thread starter =?ISO-8859-1?Q?Thomas_Gagn=E9?=
  • Start date
?

=?ISO-8859-1?Q?Thomas_Gagn=E9?=

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
 
M

Marco Schmidt

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
 
?

=?ISO-8859-1?Q?Thomas_Gagn=E9?=

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!
 
I

Ian McCall

Thomas Gagné said:
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
 
A

A. Bolmarcich

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.
 
I

Ian McCall

A. Bolmarcich said:
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
 
J

John C. Bollinger

Thomas said:
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
(e-mail address removed)
 
?

=?ISO-8859-1?Q?Thomas_Gagn=E9?=

John said:
<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.
 
J

Jon Skeet

Ian McCall said:
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.
 
I

Ian McCall

Jon Skeet said:
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
 
J

Jon Skeet

Ian McCall said:
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 :)
 
J

Jonathan Oexner

Ian McCall said:
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?
 
?

=?ISO-8859-1?Q?Thomas_Gagn=E9?=

Jonathan said:
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.
 

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

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top