answersLogoWhite

0

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

13y ago

What else can I help you with?

Related Questions

What happens when you pass an object as an argument to addAll fucntion of arraylist?

If the passed object extends Collection, then all the objects in collection are added to the arraylist.


Collection framework in java?

Collection framework is a framework in java that helps us handle multiple java objects in one shot. For example if you have an employee validation system where you have details about all the employees in an office, you will have lets say 1000 employee objects available in an ArrayList which we can iterate and check if every employee that is going through the door is a valid employee. Some of the collections we can use are: a. ArrayList b. Vector c. HashMap d. HashSet e. etc


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.


How can you convert ArrayList to Vector?

Both of these types of Collections allow for a new instance to be created with the contents of another Collection. // This method will accept a Vector and return a new ArrayList which contains all elements of that Vector static ArrayList toArrayList(Vector v) { return new ArrayList(v); } // This method will accept an ArrayList and return a new Vector which contains all elements of that ArrayList static Vector toArrayList(ArrayList al) { return new Vector(al); }


Which collection classes allows you to grow or shrink its size and provides indexed access to its elements but whose methods are not synchronized?

ARRAYLIST


What is an Arraylist used for?

An ArrayList is a data structure. It stores data in an array that can be dynamically resized. This data structure in Microsoft .NET Framework contains Methods that assist the programmer in accessing and storing data within the ArrayList. The following link explains the .NET ArrayList class. http://msdn2.microsoft.com/en-us/library/system.collections.arraylist(vs.80).aspx


Define an arraylist with an example of arraylist then you can understand it?

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.


Define iterator in java?

An Iterator is a java feature using which we can loop through every single element of a collection and perform operations on them. It is extremely useful because we don't have to worry about the collection size or whether we will get index out of bounds exceptions while looping through a collection Ex: ArrayList lst = new ArrayList(); //code that populates the list Iterator ir = lst.iterator(); Here itr is the iterator that will help the code loop through each element of the collection.


How do you use arraylist in java programming?

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.


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.


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.


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);