60 likes | 185 Views
Array in Java. Array, element, index(or subscript) int[] a = new int[12]; int[] a = {31, 28, 31, 30, 31, 30 31, 31, 30, 31, 30, 31}; a[0], a[1],…, and a[11] a.length String[] d = {“Sun”, “Mon”, Tue”, “Wed”, “Thu”, “Fri”, “Sat”};
E N D
Array in Java • Array, element, index(or subscript) • int[] a = new int[12]; • int[] a = {31, 28, 31, 30, 31, 30 31, 31, 30, 31, 30, 31}; • a[0], a[1],…, and a[11] • a.length • String[] d = {“Sun”, “Mon”, Tue”, “Wed”, “Thu”, “Fri”, “Sat”}; • public static void main(String[] args) {}
Array in Java • Multidimensional array • int[][] b = new int[4][3]; • int[][] b = {{1, 2, 3}, {4, 5, 6} {7, 8, 9}, {0, 1, 2}}; • b[0][0], b[0][1],…, b[3][2] • b[0], b[1], b[2], b[3] • b.length(the number of rows), b[0].length,…, b[3].length
Collections in Java • Generic Collections • ArrayList • LinkedList • List interface
Collection: ArrayList • ArrayList (java.util package) • ArrayList<String> list1 = new ArrayList<String>(); • Methods: size, get, add(with/without index), remove, clear
Collection: LinkedList • LinkedList(java.util package) • List<String> list2 = new LinkedList<String>(); • Interface: List (search interface list in java) • Iterator: ListIterator<String> itr =list2.listIterator(); • itr.hasNext(), itr.next(), itr.hasPrevious(), itr.previous()