-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArrayList.java
More file actions
23 lines (17 loc) · 892 Bytes
/
ArrayList.java
File metadata and controls
23 lines (17 loc) · 892 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
import java.util.ArrayList;
class Test{
public static void main(String args[])
{
//Basically a re-sizeable array. Use reference data types eg: String,Integer,Double etc.
ArrayList<String> food = new ArrayList<String>();
food.add("Hamburger"); //Adds the term "Hamburger".
food.add("Pizza");
food.add("Pasta");
for(int i=0; i<food.size(); i++) //size() : gets the size of the array list.
{System.out.println(food.get(i));} //get(index) : gets the element at the specified index.
food.set(0,"Chocolate"); //set() : Sets the element at 0 as "Chocolate".
System.out.println(food); // Prints the elements in a List order with [] brackets.
food.remove(2); // remove() : removes the element at index 2.
food.clear(); // clear() : Clears the whole Array List.
}
}