answersLogoWhite

0


Best Answer

The java.util.ArrayList class is one of the most commonly used of all the classes in the Collections Framework. Some of the advantages ArrayList has over arrays are

• It can grow dynamically.

• It provides more powerful insertion and search mechanisms than arrays.

Let's take a look at using an ArrayList that contains Strings. A key design goal of the Collections Framework was to provide rich functionality at the level of the main interfaces: List, Set, and Map. In practice, you'll typically want to instantiate an ArrayList polymorphically like this:

List myFirstArrayList = new ArrayList();

As of Java 5 you'll want to say

List myFirstArrayList = new ArrayList();

This kind of declaration follows the object oriented programming principle of "coding to an interface", and it makes use of generics. We'll say lots more about generics in future, but for now just know that, as of Java 5, the syntax is the way that you declare a collection's type. (Prior to Java 5 there was no way to specify the type of a collection, and when we cover generics, we'll talk about the implications of mixing Java 5 (typed) and pre-Java 5 (untyped) collections.)

In many ways, ArrayList is similar to a String[] in that it declares a container that can hold only Strings, but it's more powerful than a String[]. Let's look at some of the capabilities that an ArrayList has

List test = new ArrayList();

String s = "hi";

test.add("string");

test.add(s);

test.add(s+s);

System.out.println(test.size());

System.out.println(test.contains(42));

System.out.println(test.contains("hihi"));

test.remove("hi");

System.out.println(test.size());

which produces

3

false

true

2

There's lots going on in this small program. Notice that when we declared the ArrayList we didn't give it a size. Then we were able to ask the ArrayList for its size, we were able to ask it whether it contained specific objects, we removed an object right out from the middle of it, and then we rechecked its size.

User Avatar

Wiki User

12y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: How do you use arraylist in java programming?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

What are the requirements to download a java arraylist?

The requirements to download a java arraylist are a pc with java software installed. A java arraylist is used to store a group of elements in a specific order.


What type of platform does Arraylist Java run on?

Arraylist Java runs on Oracle which is a relational data management database produced by the Oracle Corporation. Arraylist Java has been part of the Java framework ever since Java 5.


Use of java.util package?

The java.util package contains many useful utilities provided by the Java programming language. They include:Collections - ArrayList, Vector, HashMap etcEvent modelsDate & time featuresString tokenizerRandom number generatoretc.


How do you sort the integer numbers of arraylist in java?

You can also use the Collections.sort() method to sort values in an array list. You can also use the Comparable Interface or Comparators for providing custom implementations of sorting algorithms for values inside an ArrayList.


What programming language does minecraft use?

Java


How do you Implement a Stack using an ArrayList in java?

Just add the elements to the end of the ArrayList, and take them out from the end of the ArrayList. Use the .add() method to add an element to the end; use the .size() method to find out how many elements it has (so you can access the last element), and use the .remove() method to eliminate this last element.


Is it possible to use arrays when using the java programming language?

It is possible to use arrays when employing java programming language. There are many different series of programming choice that can be employed with various end results.


What advantage does the Java ArrayList class have over the Arrays class?

The biggest advantage of an ArrayList is that it can expand in size to fit more data. So, if you don't know how many data values you are going to have, you can construct an ArrayList. Whenever you use the add() method, the object will be added to the ArrayList, regardless of the current size. An Array does not have this advantage. When you construct an Array of size n, the array will always be that size.


How can you create array of an object in java?

Array's can hold only primitive data types. if you want a collection of objects you must use an ArrayList or a Vector.


How can you sort an arraylist of strings?

You can sort an ArrayList by using the sort method of the Collecions class (java.util.Collections). Assuming you have an ArrayList called foo: Collections.sort(foo);


What programming language do most steam games use?

java


What is subclasses in java?

Subclasses are classes that inherit from parent classes. i.e. ArrayList is a subclass of List.