ArrayList Basics

PHOTO EMBED

Sun Jan 16 2022 05:56:14 GMT+0000 (Coordinated Universal Time)

Saved by @mikemcnik #java

import java.util.ArrayList;
ArrayList<String> names = new ArrayList<>();
names.add("Emily");
names.add("Bob");
names.add("Cindy");

int n = names.size();  // Returns n = 3
String s = names.get(0); //Assigns "Emily" to s
String s = names.set(2, "Carolyn"); //Assigns "Cindy" to s, since it was the value that was replaced

Names.add(1, "Ann"); //inserts the element at the given index.  The index shifts to add the given element
String S = names.remove(1); //Removes element at given index.  Element is saved as s.

for (int I = 0; I < names.size(); ++i){
	System.out.println(names.get(i));
}

for (String name : names){
	System.out.println(name); //Enhanced For Loop
}
content_copyCOPY

Builds a basic ArrayList