4

I am writing a program that will be heavily reliant on ... something ... that stores data like an array where I am able to access any point of the data at any given time as I can in an array.

I know that the java library has an Array class that I could use or I could use a raw array[].

I expect that using the Array type is a bit easier to code, but I expect that it is slightly less efficient as well.

My question is, which is better to use between these two, and is there a better way to accomplish the same result?

9
  • What exactly are you trying to do? Do you know how many elements are stored in advance? Commented Sep 8, 2011 at 3:51
  • 1
    Do you really mean Array, or do you mean ArrayList? Commented Sep 8, 2011 at 3:51
  • Is this the Array you're referring to? This is just a utility class. Commented Sep 8, 2011 at 3:53
  • @Gabe +1 from the sound of it ArrayList is probably what is meant. Commented Sep 8, 2011 at 3:55
  • I really did mean Array, but I was mistaken about the usage for it. ArrayList is what I should have meant. Commented Sep 8, 2011 at 3:59

4 Answers 4

5

Actually Array would be of no help -- it's not what you think it is. The class java.util.ArrayList, on the other hand, is. In general, if you can program with collection classes like ArrayList, do so -- you'll more easily arrive at correct, flexible software that's easier to read, too. And that "if" applies almost all the time; raw arrays are something you use as a last resort or, more often, when a method you want to call requires one as an argument.

Sign up to request clarification or add additional context in comments.

1 Comment

Ah. Thanks, ArrayList is what I am looking for and this answers my question.
3

The Array class is used for Java reflection and is very, very, rarely used.

If you want to store data in an array, use plain old arrays, indicated with [], or as Gabe's comment on the question suggests, java.util.ArrayList. ArrayList is, as your comment suggests easier to code (when it comes to adding and removing elements!!) but yes, is slightly less efficient. For variable-size collections, ArrayList is all but required.

Comments

2

My question is, which is better to use between these two, and is there a better way to accomplish the same result?

It depends on what you are trying to achieve:

  • If the number of elements in the array is known ahead of time, then an array type is a good fit. If not, a List type is (at least) more convenient to use.

  • The List interface offers a number of methods such as contains, insert, remove and so on that can save you coding ... if you need to do that sort of thing.

  • If properly used, an array type will use less space. The difference is particularly significant for arrays of primitive types where using a List means that the elements need to be represented using wrapper types (e.g. byte becomes Byte).


The Array class is not useful in this context, and neither is the Arrays class. The choice is between ArrayList (or some other List implementation class) and primitive arrays.

Comments

-2

In terms of ease of use, the Array class is a lot easier to code. The array[] is quite a problem in terms of the case that you need to know the size of the list of objects beforehand.

Instead, you could use a HashMap. It is very efficient in search as well as sorting as the entire process is carried out in terms of key values.

You could declare a HashMap as:

HashMap<String, Object> map = new HashMap<String, Object>();

For the Object you can use your class, and for key use the value which needs to be unique.

2 Comments

Why is it down voted? Didn't the asker ask for an alternate way?
-1 "the Array class is a lot easier to code" - Array is a utility class, and it's been established the OP meant ArrayList. Also, HashMap provides features the OP doesn't need and is no longer ordered like an array or List.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.