
An array is an object
which holds 0 or more
objects or primitives

int[] intArray; //single-dimensional array of intsint intArray[]; //also a 1D array of ints
int[][] intArray; //two-dimensional array of intsDouble doubleArray[][][]; //3D array of Double objects
int ints[]; // declarationints = new int[5]; // allocation
double[][] coords; // declarationcoords = new double[3][4]; // allocation
Random r = new Random();int[] whoKnowsHowManyInts = new int[r.nextInt()];
int[] noInts = new int[0];// leave one or more sizes off at the endint[][][] threeDInt = new int[3][][];// allocate from the left to the rightfor(int i = 0; i < threeDInt.length; i++) {threeDInt[i] = new int[i + 1][];for(int j = 0; j < threeDInt[i].length; j++) {threeDInt[i][j] = new int[2 + j];}}
int[5] fiveInts; // compilation errordouble[4] fourDoubles = new double[4]; // compilation error
int[] twoAndAHalfInts = new int[2.5]; // compilation errorint[] negArray = new int[-1]; // runtime errorint[][] twoDArray = new int[][3]; // compile errorint[][][] threeDArray = new int[2][][3]; // compile error
You can init with a loop:
int[] ints = new int[3];for(int i = 0; i < ints.length; i++) {ints[i] = i * 10;}
You can init with a snoop:
String[] snoops = new String[2];snoops[0] = "dog";snoops[1] = "lion"
You can new things from thin air:
Set<String>[] sets = new Set[2];
sets[0] = new TreeSet<String>();
sets[1] = new HashSet<String>();
You can store things anywhere:
int[] bunchOfInts = new int[100];bunchOfInts[36] = 92;bunchOfInts[84] = 6;bunchOfInts[2] = 83765;
int intArray[] = {0, 1};
String[] strArray = {"Summer", "Winter"};
int multiArray[][] = { {0, 1}, {3, 4, 5} };int intArray2[] = new int[]{0, 1};
String[] strArray2 = new String[]{"Summer", "Winter"};
int multiArray2[][] = new int[][]{ {0, 1}, {3, 4, 5}};// these don't compile
int intArray2[] = new int[2]{0, 1};
String[] strArray2 = new String[2]{"Summer", "Winter"};
int multiArray2[][] = new int[2][]{ {0, 1}, {3, 4, 5}};
int[][][] arr = {{null}, null, {{3, 4, 5}}};
int i = someInts[1];Object o = strings[3];
int arNeg = ar[-1]; // throws ArrayIndexOutOfBoundsException
int arPartEnd = ar[10]; // throws if the size is less than 11
int fracIndex = intArray[2.4]; // doesn't compile
int[] arr = somethingWhichAllocatesArray();for(int i = 0; i < arr.length; i++) {System.out.println(arr[i]);if(i == arr.length / 2) {System.out.println("Half-way");}}
int[] arr = somethingWhichAllocatesArray();for(int arrItem : arr) {System.out.println(arrItem);// no way of getting the position in here, only the value}
Line1> String multiStrArr[][] = new String[][] {
Line2> {"A", "B"},
Line3> null,
Line4> {"Jan", "Feb", null},
Line5> };
interface MyInterface {}
class MyClass1 implements MyInterface {}
class MyClass2 implements MyInterface {}
class Test {
MyInterface[] interfaceArray = new MyInterface[]
{
new MyClass1(),
null,
new MyClass2()
};
}
abstract class Vehicle{}
class Car extends Vehicle {}
class Bus extends Vehicle {}
class Test {
Vehicle[] vehicleArray = {
new Car(),
new Bus(),
null
};
}interface MyInterface {} class MyClass1 implements MyInterface {} abstract class Vehicle{} class Car extends Vehicle {}class Test { Object[] objArray = new Object[] { new MyClass1(), null, new Car(), new java.util.Date(), new String("name"), 3, // this gets autoboxed into an Integer new Integer [7] // arrays are objects, too! }; }
import java.util.ArrayList;
public class CreateArrayList {
public static void main(String args[]) {
ArrayList<String> myArrList = new ArrayList<String>();
}
}import java.util.ArrayList;
public class CreateArrayList {
public static void main(String args[]) {
ArrayList<String> myArrList = new ArrayList<>();
}
}private transient Object[] elementData;
import java.util.ArrayList;
public class AddToArrayList {
public static void main(String args[]) {
ArrayList<String> list = new ArrayList<>();
// add at the end
list.add("one"); // {"one"}
list.add("two"); // {"one", "two"}
list.add("four"); // {"one", "two", "four"}
// add at the specified position, shifting anything there over
list.add(2, "three"); // {"one", "two", "three", "four"}
}
}
import java.util.ArrayList;
public class AccessArrayList {
public static void main(String args[]) {
ArrayList<String> myArrList = new ArrayList<>();
myArrList.add("One");
myArrList.add("Two");
myArrList.add("Four");
myArrList.add(2, "Three");
for (String element : myArrList) {
System.out.println(element);
}
}
}
import java.util.ArrayList;
import java.util.ListIterator;
public class AccessArrayListUsingListIterator {
public static void main(String args[]) {
ArrayList<String> myArrList = new ArrayList<String>();
myArrList.add("One");
myArrList.add("Two");
myArrList.add("Four");
myArrList.add(2, "Three");
ListIterator<String> iterator = myArrList.listIterator();
while (iterator.hasNext()) {
System.out.println(iterator.next());
}
}
}
import java.util.ArrayList;
public class ReplaceElementInArrayList {
public static void main(String args[]) {
ArrayList<String> myArrList = new ArrayList<String>();
myArrList.add("One");
myArrList.add("Two");
myArrList.add("Three");
myArrList.set(1, "One and Half");
for (String element : myArrList) {
System.out.println(element);
}
}
}import java.util.ArrayList; public class ModifyArrayListWithStringBuilder { public static void main(String args[]) { ArrayList<StringBuilder> myArrList = new ArrayList<StringBuilder>(); myArrList.add(new StringBuilder("One")); myArrList.add(new StringBuilder("Two")); myArrList.add(new StringBuilder("Three")); for (StringBuilder element : myArrList) { element.append(element.length()); }for (StringBuilder element : myArrList) { System.out.println(element); } } }
ArrayList<String> myArrList = new ArrayList<String>(); String one = "One"; String two = new String("Two"); myArrList.add(one); myArrList.add(two);ArrayList<String> yourArrList = myArrList; one.replace("O", "B");for (String val : myArrList) { System.out.print(val + ":"); } for (String val : yourArrList) { System.out.print(val + ":"); }
ArrayList<StringBuilder> myArrList = new ArrayList<>();
StringBuilder sb1 = new StringBuilder("Jan");
StringBuilder sb2 = new StringBuilder("Feb");
myArrList.add(sb1);
myArrList.add(sb2);
ArrayList<StringBuilder> clonedArrList = (ArrayList<StringBuilder>)myArrList.clone();
System.out.println(clonedArrList == myArrList); // false
System.out.println(clonedArrList.equals(myArrList)); // true
System.out.println(sb1 == clonedArrList.get(0)); // true, same ref
System.out.println(sb2 == clonedArrList.get(1)); // true, same ref
ArrayList<StringBuilder> myArrList = new ArrayList<>();
StringBuilder sb1 = new StringBuilder("Jan");
StringBuilder sb2 = new StringBuilder("Feb");
myArrList.add(sb1);
myArrList.add(sb2);
ArrayList<StringBuilder> clonedArrList = new ArrayList<>(myArrList);
System.out.println(clonedArrList == myArrList); // false
System.out.println(clonedArrList.equals(myArrList)); // true
System.out.println(sb1 == clonedArrList.get(0)); // true, same ref
System.out.println(sb1 == clonedArrList.get(0)); // true, same refArrayList<StringBuilder> clonedArrList = new ArrayList<>();
clonedArrList.addAll(myArrList);
ArrayList<String> myArrList = new ArrayList<>();
myArrList.add("Hi");
myArrList.add("Dudes");
String[] myArr = (String[]) myArrList.toArray();