一
数组的定义方式:
1 intdata[] = new int[x]
2 int data[] = new int[] {1, 2, 3, 4 …}
二
数组的排序: ( java.util.Arrays.sort() )
/*
public static void sort(int temp[]) {
for(inti = 0; i < temp.length; i++) {
for(intj = 0; j < temp.length - 1; j++) {
if(temp[j]> temp[j+1]) {
intp = temp[j];
temp[j] = temp[j+1];
temp[j+1] = p;
}
}
}
}
*/
三
数组的转置:1, 2, 3, 4, 5 >> 5, 4, 3, 2, 1
public staticvoid reverse(int arr[]) {
intlen = arr.length/2;
inthead = 0;
inttill = arr.length - 1;
for(inti = 0; i < len; i++) {
inttemp = arr[head];
arr[head] = arr[till];
arr[till] = temp;
head++;
till--;
}
}
(!)四
对象数组:将多个对象交给数组统一管理。
class Book {
private String name;
private double price;
public Book(String n, double p) {
name = n;
price = p;
}
public String getInfo() {
return"Name: " + name + " price: " + price;
}
}
public class StringArrays {
public static void main(String args[]) {
Bookbk[] = new Book[] {
newBook("Java", 23.2),
newBook("Jsp", 25.3),
newBook("Jdk", 53.1)
};
for(inti = 0; i < bk.length; i++) {
System.out.println(bk[i].getInfo());
}
}
}