arralist in java code example

Example 1: java arraylist

import java.util.List; //list abstract class
import java.util.ArrayList; //arraylist class

//Object Lists
List l = new ArrayList();
ArrayList a = new ArrayList();

//Specialized List
List l = new ArrayList();
ArrayList a = new ArrayList();
//only reference data types allowed in brackets <>

//Initial Capacity
List l = new ArrayList(5);
//list will start with a capacity of 5
//saves allocation times

Example 2: arraylist java methds

import java.util.ArrayList;
ArrayList myList = new ArrayList();
myList.add(0);
myList.remove(0);//Remove at index 0
myList.size();
myList.get(0);//Return element at index 0

Example 3: java arraylist

List expenseTypeList = new ArrayList<>();
        expenseTypeList.add("GENERAL");
        expenseTypeList.add("PURCHASE");
        expenseTypeList.add("PROJECT");

Example 4: java araylist

import java.util.ArrayList; // import the ArrayList class

ArrayList cars = new ArrayList(); // Create an ArrayList object

Tags:

Misc Example