fundamendal question

Hi guys,

I have a simp[le question though troubling.

In other languages you can declare an array object and then specifiy the index you want to retrieve by doing:

var value = Array[i]

I java you have to get it by calling the .get(i); method.

Can someone explain to me what the benefit of having a .get() method is?

Thanks,

Theo

[376 byte] By [Gorteoa] at [2007-11-27 11:03:43]
# 1

What are you talking about?

This is Javaint[] array = new int[] {1,2,3};

int secondElement = array[1];

dwga at 2007-7-29 12:52:08 > top of Java-index,Java Essentials,Java Programming...
# 2

Perhaps you're thinking in ArrayList

manuel.leiriaa at 2007-7-29 12:52:08 > top of Java-index,Java Essentials,Java Programming...
# 3

> Perhaps you're thinking in ArrayList

Which is not an array.

dwga at 2007-7-29 12:52:08 > top of Java-index,Java Essentials,Java Programming...
# 4

> Perhaps you're thinking in ArrayList

Some of the advantages of ArrayList are:

It extends automatically if you add more elements than its original capacity.

It has an API documented in the API docs in a way that is similar to hundreds of other classes and therefore familiar to Java programmers.

You may argue that the array could have been made extensible the way ArrayList is. But that would hurt the efficiency of the static (fixed-size) array. The advantage of not doing that is you have a choice between the efficient array and the more programmer-friendly ArrayList.

HTH

OleVVa at 2007-7-29 12:52:08 > top of Java-index,Java Essentials,Java Programming...
# 5

> It has an API documented in the

> API docs in a way that is similar

> to hundreds of other classes and

> therefore familiar to Java programmers.

You mean to say that arrays need documentation and since ArrayList has documentation it's better?

qUesT_foR_knOwLeDgea at 2007-7-29 12:52:08 > top of Java-index,Java Essentials,Java Programming...
# 6

> You mean to say that arrays need documentation and

> since ArrayList has documentation it's better?

No, I didn't mean to say that.

Arrays are documented elsewhere, in the language specification and in about any Java textbook.

ArrayList and array each has its benefits and limitations. I sometimes use ArrayList, at other times array, depending on the situation and requirements. When I taught Java for beginners, I tried to avoid talking about arrays since I thought ArrayList would make an easier start (learning curve) and students interested in arrays would be able to read the text book and learn on their own. A professional Java programmer should certainly be able to use both.

I hope this answers your question?

OleVVa at 2007-7-29 12:52:08 > top of Java-index,Java Essentials,Java Programming...
# 7

> Perhaps you're thinking in ArrayList

Perhaps perhaps you were also thinking of the operator overloading found in C++ (and C#, I believe). Since [] is considered an operator, that allows you to use the [] syntax with collection classes (and any classes at all, really):

int value = myIntegerArrayList[i];

I'm into the serious mind reading here. As you may have understood by now, your question didn't make much sense. I really can't do mind reading over the net, but I'm trying anyway, so forgive me if I'm far off, and just ignore what I say. :-)

I didn't listen closely when they told me the reasons why they don't have operator overloading in Java. In my opinion, there are very few examples where operator overloading allows a considerably more readable program, while there are very many examples where it really confuses more than it helps. So I don't yearn for operator overloading in Java. I believe some do. In other words, the .get(i) syntax is a minute disadvantage. Operator overloading would be a major complication of the language.

Java does have method overloading, that's more than enough for me. There are examples where I enjoy constructor overloading, since objects of a class may be constructed from very different data. I don't use method overloading for much other than this.

OleVVa at 2007-7-29 12:52:08 > top of Java-index,Java Essentials,Java Programming...