Java Programming: Arrays

Download Report

Transcript Java Programming: Arrays

Java Programming:
Arrays
Vyacheslav Grebenyuk
CTDE, AI dept., KhNURE
Content
Creating and Using Arrays
 Arrays of Objects
 Arrays of Arrays
 Copying Arrays
 Summary of Arrays

(С) ЦТДО, каф. Искусственного интеллекта, ХНУРЭ, 2006
2
Arrays
An array is a structure that holds multiple
values of the same type
 The length of an array is established when
the array is created
 After creation, an array is a fixed-length
structure

(С) ЦТДО, каф. Искусственного интеллекта, ХНУРЭ, 2006
3
Creating and Using Arrays
public class ArrayDemo {
public static void main(String[] args) {
int[] anArray;
// declare an array of integers
anArray = new int[10]; // create an array of integers
// assign a value to each array element and print
for (int i = 0; i < anArray.length; i++) {
anArray[i] = i;
System.out.print(anArray[i] + " ");
}
System.out.println();
}
}
(С) ЦТДО, каф. Искусственного интеллекта, ХНУРЭ, 2006
4
Declaring a Variable to Refer to
an Array
int[] anArray;
 float[] anArrayOfFloats;
 boolean[] anArrayOfBooleans;
 Object[] anArrayOfObjects;
 String[] anArrayOfStrings;


float anArrayOfFloats[];
(С) ЦТДО, каф. Искусственного интеллекта, ХНУРЭ, 2006
5
Creating an Array
anArray = new int[10];
 Array Initializers
boolean[] answers = { true, false, true,
true, false };

(С) ЦТДО, каф. Искусственного интеллекта, ХНУРЭ, 2006
6
Accessing an Array Element and
Getting the Size of an Array
for (int i = 0; i < anArray.length; i++) {
anArray[i] = i;
System.out.print(anArray[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 ArrayOfArraysDemo {
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 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 ArrayOfArraysDemo2 {
public static void main(String[]
args) {
int[][] aMatrix = new int[4][];
//populate matrix
for (int i = 0; i <
aMatrix.length; i++) {
//create sub-array
aMatrix[i] = new int[5];
for (int j = 0; j <
aMatrix[i].length; j++) {
aMatrix[i][j] = i + j;
}
}
//print matrix
for (int i = 0; i <
aMatrix.length; i++) {
for (int j = 0; j <
aMatrix[i].length; j++) {
System.out.print(
aMatrix[i][j] + " ");
}
System.out.println();
}
}
}
01234
12345
23456
34567
(С) ЦТДО, каф. Искусственного интеллекта, ХНУРЭ, 2006
9
Copying Arrays

System
public static
void arraycopy(Object source,
int srcIndex,
Object dest,
int destIndex,
int length,
(С) ЦТДО, каф. Искусственного интеллекта, ХНУРЭ, 2006
10
Copying Arrays 2
public class ArrayCopyDemo {
public static void main(String[] args) {
char[] copyFrom = { 'd', 'e', 'c', 'a', 'f', 'f', 'e',
'i', 'n', 'a', 't', 'e', 'd' };
char[] copyTo = new char[7];
System.arraycopy(copyFrom, 2, copyTo, 0, 7);
System.out.println(new String(copyTo));
}
}
(С) ЦТДО, каф. Искусственного интеллекта, ХНУРЭ, 2006
11