Download as pdf or txt
Download as pdf or txt
You are on page 1of 2

Andrew Nickerson

Period 5 - 1/9/19
AndrewN_ArrayListLab

import java.util.ArrayList;

public class AndrewNArrayListLab {

public static void main(String[] args) {


ArrayList list = new ArrayList();

list.add("Andrew Nickerson");
list.add(16);
list.add(1.0);
list.add(new BankAccount(9090));
list.add(new BankAccount(1234));
System.out.println(list);

list.add(1, "A2");
System.out.println(list);
System.out.println("The list has "+ list.size() + " elements in it.");
System.out.println();
Object found = null;
boolean isFound = false;
for(Object a: list){
System.out.println(a);
if(a instanceof String){
if(((String)a).equals("A2")){
found = a;
isFound = true;
System.out.println("found A2");

}
}
}
System.out.println();
if(isFound){
list.remove(found);
System.out.println("This is the new list:");
System.out.println(list);
}

list.set(1, 2020);
System.out.println(list);
Andrew Nickerson
Period 5 - 1/9/19

System.out.println("The last element: " + list.get(list.size()-1));

list.set(3, ((BankAccount)list.get(3)).getAccountNumber());
list.set(4, ((BankAccount)list.get(4)).getAccountNumber());
System.out.println(list);

OUTPUT:

You might also like