• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Array vs. ArrayList

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

nil wangad wrote:
how to create dynamic string array if we dont know number of strings in the beginning?



Java has arrays that can grow, like ArrayList.

Just declare the ArrayList to be of String and you can add as many String objects you want.
 
Bartender
Posts: 2236
63
IntelliJ IDE Firefox Browser Spring Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Lindy Lindqvist wrote:

nil wangad wrote:
how to create dynamic string array if we dont know number of strings in the beginning?


Java has arrays that can grow, like ArrayList.



Technically, you are wrong. ArrayList is not an array. It is a list. The fact that is based on array does not matter.
Arrays in Java can't grow.
ArrayList uses an array internally. If you want do add an element and the array is too small, ArrayList will copy all its contents to bigger array.
 
Lindy Lindqvist
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Pawel Pawlowicz wrote:
Technically, you are wrong. ArrayList is not an array. It is a list.



No I'm technically right and you are nitpickingly wrong.

The ArrayList is a dynamic array implementation part of the Java standard API. Dynamic means it can grow at runtime.
 
Paweł Baczyński
Bartender
Posts: 2236
63
IntelliJ IDE Firefox Browser Spring Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Lindy Lindqvist wrote:

Pawel Pawlowicz wrote:
Technically, you are wrong. ArrayList is not an array. It is a list.



No I'm technically right and you are nitpickingly wrong.

The ArrayList is a dynamic array implementation part of the Java standard API. Dynamic means it can grow.


Yes, ArrayList can grow.
No, ArrayList is not an array. It is a List implementation that uses an array internally.
In Java arrays have fixed size. They can't grow. Even those used by ArrayList!
If an ArrayList finds out that its array is too small it creates larger one (still fixed size!) and forgets about the smaller one.
And yes, I am nitpicking
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Lindy Lindqvist wrote:No I'm technically right and you are nitpickingly wrong.


Sorry, but you are not correct. ArrayList is not an array; it is a List implementation. With a List (ArrayList included), you cannot use array indexing, and there is no length property.

Saying that ArrayList is an array is not just technically incorrect, it's even conceptually incorrect.
 
Lindy Lindqvist
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Pawel Pawlowicz wrote:
No, ArrayList is not an array.



Yes it is. It's a dynamic array.

You're confusing design with implementation.

 
Lindy Lindqvist
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Bear Bibeault wrote:

Lindy Lindqvist wrote:No I'm technically right and you are nitpickingly wrong.


Sorry, but you are not correct. ArrayList is not an array; it is a List implementation. With a List (ArrayList included), you cannot use array indexing, and there is no length property.

Saying that ArrayList is an array is not just technically incorrect, it's even conceptually incorrect.



Don't confuse the List interface with the ArrayList concrete class.

The ArrayList implements the List interface for sure but that doesn't change the fact that ArrayList is a dynamic array implementation.

So you're wrong. The ArrayList is a dynamic array, nothing else. Period.
 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If x is a reference to an ArrayList, and it's an array (according to you), then shouldn't we be able to write "x[2]" and "x.length"? After all you can do that for arrays.

But you can't. So it isn't correct to say an ArrayList is an array. It may well be correct that an ArrayList has an array, but that isn't the same thing. The distinction is well-known in the field of object-oriented design.
 
Paweł Baczyński
Bartender
Posts: 2236
63
IntelliJ IDE Firefox Browser Spring Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Trying to compile this:results in

Tmp.java:6: error: inconvertible types
System.out.println(a instanceof Object[]);
^
required: Object[]
found: ArrayList


Is Java compiler also wrong?

Note: Object[] is a superclass for all arrays in Java


Give it a chance:
Tmp.java:7: error: array required, but ArrayList found
System.out.println(a[0]);


Nope. ArrayList is not an array.
 
Lindy Lindqvist
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Paul Clapham wrote:If x is a reference to an ArrayList, and it's an array (according to you), then shouldn't we be able to write "x[2]" and "x.length"? After all you can do that for arrays.

But you can't. So it isn't correct to say an ArrayList is an array. It may well be correct that an ArrayList has an array, but that isn't the same thing. The distinction is well-known in the field of object-oriented design.



Well, I said ArrayList is a dynamic array implementation. And it is. I didn't claim ArrayList would be exacly like a native Java array. The Java language isn't flexible enougth to make such an implementation possible.

But okay I'll rephrase. The ArrayList is a dynamic array implementation but it's only as close to a native Java array as the Java language permits (and in that sense ArrayList doesn't differ from any other class in Java that's not part of the core language or has special language support).
 
Lindy Lindqvist
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Pawel Pawlowicz wrote:
Nope. ArrayList is not an array.



Yes it is.

ArrayList is a dynamic array implementation and it's as close to a native array as the Java language permits a class to be. And it's part of the standard API.

This means when a static Java array doesn't suit you, first have a look at the ArrayList.

 
Paweł Baczyński
Bartender
Posts: 2236
63
IntelliJ IDE Firefox Browser Spring Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Lindy Lindqvist wrote:

Pawel Pawlowicz wrote:
Nope. ArrayList is not an array.



Yes it is.

ArrayList is a dynamic array implementation and it's as close to a native array as the Java language permits a class to be. And it's part of the standard API.

This means when a static Java array doesn't suit you, first have a look at the ArrayList.


Technically, static in Java means... Ehhh. Never mind...
 
Lindy Lindqvist
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Pawel Pawlowicz wrote:
Technically, static in Java means... Ehhh. Never mind...



You don't know the difference between a static and a dynamic array?

Then why did you enter this discussion at all. To nitpick?

In that case why don't you complain where it's due, namely at the Java language.

The ArrayList is a dynamic array implementation but it still lacks many features of a native Java array. Why? Simply because Java doesn't allow it to get closer. Java doesn't support things like operator overloading for example. That would be something of substance to complain about.

Furthermore don't even try besserwisser insinuations with me. Good programmers easily can jump between the general notion of a static array and specific uses of the static keyword in Java. Or are you challenged for real?
 
Sheriff
Posts: 3837
66
Netbeans IDE Oracle Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I've extracted this conversation from here.

Please continue the array vs. ArrayList discussion here if you must, but at this point I'd suggest to consult the JLS to anyone who wants to get an authoritative answer about what the term "array" refers to in Java.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic