Java Programming Arrays Vyacheslav Grebenyuk CTDE AI dept
Java Programming: Arrays Vyacheslav Grebenyuk CTDE, AI dept. , Kh. NURE (С) ЦТДО, каф. Искусственного интеллекта, ХНУРЭ, 2006
Content Creating and Using Arrays n Arrays of Objects n Arrays of Arrays n Copying Arrays n Summary of Arrays n (С) ЦТДО, каф. Искусственного интеллекта, ХНУРЭ, 2006 2
Arrays An array is a structure that holds multiple values of the same type n The length of an array is established when the array is created n After creation, an array is a fixed-length structure n (С) ЦТДО, каф. Искусственного интеллекта, ХНУРЭ, 2006 3
Creating and Using Arrays public class Array. Demo { public static void main(String[] args) { int[] an. Array; // declare an array of integers an. Array = new int[10]; // create an array of integers // assign a value to each array element and print for (int i = 0; i < an. Array. length; i++) { an. Array[i] = i; System. out. print(an. Array[i] + " "); } System. out. println(); } } (С) ЦТДО, каф. Искусственного интеллекта, ХНУРЭ, 2006 4
Declaring a Variable to Refer to an Array int[] an. Array; n float[] an. Array. Of. Floats; n boolean[] an. Array. Of. Booleans; n Object[] an. Array. Of. Objects; n String[] an. Array. Of. Strings; n n float an. Array. Of. Floats[]; (С) ЦТДО, каф. Искусственного интеллекта, ХНУРЭ, 2006 5
Creating an Array an. Array = new int[10]; n Array Initializers boolean[] answers = { true, false, true, false }; n (С) ЦТДО, каф. Искусственного интеллекта, ХНУРЭ, 2006 6
Accessing an Array Element and Getting the Size of an Array for (int i = 0; i < an. Array. length; i++) { an. Array[i] = i; System. out. print(an. Array[i] + " "); } arrayname. length Be careful: Programmers new to the Java programming language are tempted to follow length with an empty set of parenthesis. This doesn't work because length is not a method. length is a property provided by the Java platform for all arrays. (С) ЦТДО, каф. Искусственного интеллекта, ХНУРЭ, 2006 7
Arrays of Arrays public class Array. Of. Arrays. Demo { public static void main(String[] args) { String[][] cartoons = { {"Flintstones", "Fred", "Wilma", "Pebbles", "Dino"}, {"Rubbles", "Barney", "Betty", "Bam Bam" }, {"Jetsons", "George", "Jane", "Elroy", "Judy", "Rosie", "Astro" }, {"Scooby Doo Gang", "Scooby Doo", "Shaggy", "Velma", "Fred", "Daphne" } }; for (int i = 0; i < cartoons. length; i++) { System. out. print( cartoons[i][0] + ": "); for (int j = 1; j < cartoons[i]. length; j++) { System. out. print( cartoons[i][j] + " "); } System. out. println(); } } } Flintstones: Fred Wilma Pebbles Dino Rubbles: Barney Betty Bam Jetsons: George Jane Elroy Judy Rosie Astro Scooby Doo Gang: Scooby Doo Shaggy Velma Fred Daphne (С) ЦТДО, каф. Искусственного интеллекта, ХНУРЭ, 2006 8
Arrays of Arrays 2 public class Array. Of. Arrays. Demo 2 { public static void main(String[] args) { int[][] a. Matrix = new int[4][]; //populate matrix for (int i = 0; i < a. Matrix. length; i++) { //create sub-array a. Matrix[i] = new int[5]; for (int j = 0; j < a. Matrix[i]. length; j++) { a. Matrix[i][j] = i + j; } } //print matrix for (int i = 0; i < a. Matrix. length; i++) { for (int j = 0; j < a. Matrix[i]. length; j++) { System. out. print( a. Matrix[i][j] + " "); } System. out. println(); } } } 012345 234567 (С) ЦТДО, каф. Искусственного интеллекта, ХНУРЭ, 2006 9
Copying Arrays n System public static void arraycopy(Object source, int src. Index, Object dest, int dest. Index, int length, (С) ЦТДО, каф. Искусственного интеллекта, ХНУРЭ, 2006 10
Copying Arrays 2 public class Array. Copy. Demo { public static void main(String[] args) { char[] copy. From = { 'd', 'e', 'c', 'a', 'f', 'e', 'i', 'n', 'a', 't', 'e', 'd' }; char[] copy. To = new char[7]; System. arraycopy(copy. From, 2, copy. To, 0, 7); System. out. println(new String(copy. To)); } } (С) ЦТДО, каф. Искусственного интеллекта, ХНУРЭ, 2006 11
- Slides: 11