7-1 ArrayDemo1.java
public class ArrayDemo1 {
	public static void main(String[] args) {
		int[] arr = new int[] { 10, 20, 30 };

		System.out.println("arrĵһԪأ" + arr[0]);
		System.out.println("arrĵڶԪأ" + arr[1]);
		System.out.println("arrĵԪأ" + arr[2]);
	}
}

7-2 ArrayDemo2.java
public class ArrayDemo2 {

	public static void main(String[] args) {
		char[] hello = { 'H', 'e', 'l', 'l', 'o', ' ', 'W', 'o', 'r', 'l', 'd' };

		System.out.println("helloĳΪ" + hello.length);
	}
}

7-3 ArrayDemo3.java
public class ArrayDemo3 {

	public static void main(String[] args) {

		int[] scores = new int[] { 82, 76, 90, 86 };
		int sum = 0;

		for (int i = 0; i < scores.length; i++) {
			sum += scores[i];	//ۼӷ
		}

		System.out.println("ܷ: " + sum);
		System.out.println("ƽ: " + (double) sum / scores.length);
	}
}

7-4 Example7_4.javaExample7_4Test.java
public class Example7_4 {

	// ˷ʵִӿ̨ݣ
	public void input(int[] arr) {

		System.out.println("5ÿһ밴س");
		try {
			byte[] buf = new byte[20];

			for (int i = 0; i < arr.length; i++) {
				System.in.read(buf);
				String str = new String(buf);
				arr[i] = Integer.parseInt(str.trim());
			}
		} catch (Exception e) {
			System.out.println("쳣ֹ");
			System.exit(0);
		}
	}

	// ˷ʵеֵ
	public int getMax(int[] arr) {
		int max = arr[0];
		for (int i = 1; i < arr.length; i++) {
			if (max < arr[i]) {
				max = arr[i];
			}
		}
		return max;
	}

	// ˷ʵֵ
	public int getSum(int[] arr) {
		int sum = 0;
		for (int i = 0; i < arr.length; i++) {
			sum += arr[i];
		}
		return sum;
	}

	// ˷ʵڿ̨ʾĸԪ
	public void echo(int[] arr) {
		System.out.println("Ԫ£");
		for (int i = 0; i < arr.length; i++) {
			System.out.print(arr[i] + " ");
		}
		System.out.println();
	}
}

public class Example7_4Test {

	public static void main(String[] args) {

		int[] array = new int[5];

		Example7_4 ob = new Example7_4();

		ob.input(array); // ӿ̨ݴarray

		int max = ob.getMax(array); // ҳarrayеֵ

		int sum = ob.getSum(array); // arrayԪصĺֵ

		ob.echo(array); // ڿ̨Գ

		System.out.println("еֵΪ" + max);
		System.out.println("ֵΪ" + sum);
	}

}

7-5 Example7_5.javaExample7_5Test.java
public class Example7_5 {
	public void selectionSort(int[] arr) {
		int minIndex;
		int t;
		for (int i = 0; i < arr.length - 1; i++) {
			minIndex = i;
			// ڲѭҳʣµСģminIndex¼
			for (int j = i + 1; j < arr.length; j++) {
				if (arr[j] < arr[minIndex]) {
					minIndex = j;
				}
			}
			// δźλԪminIndex¼µСн
			t = arr[i];
			arr[i] = arr[minIndex];
			arr[minIndex] = t;
		}
	}
}

public class Example7_5Test {

	public static void main(String[] args) {
		Example7_4 ob1 = new Example7_4(); // ҪʹExample7_4ļ

		Example7_5 ob2 = new Example7_5();

		int[] array = new int[5];

		ob1.input(array); // Example7_4inputԪ
		ob1.echo(array); // Example7_4echoʾǰ

		ob2.selectionSort(array); // 

		ob1.echo(array); // Example7_4echoʾź
	}
}

7-6 Example7_6.javaExample7_6Test.java
public class Example7_6 {

	public void bubbleSort(int[] arr) {

		for (int i = 0; i < arr.length - 1; i++) {
			for (int j = 1; j < arr.length - i; j++) {

				if (arr[j - 1] > arr[j]) {
					int t = arr[j - 1];
					arr[j - 1] = arr[j];
					arr[j] = t;
				}
			}
		}
	}
}

public class Example7_6Test {

	public static void main(String[] args) {
		Example7_4 ob1 = new Example7_4(); // ҪʹExample7_4ļ

		Example7_6 ob2 = new Example7_6();

		int[] array = new int[5];

		ob1.input(array); // Example7_4inputԪ
		ob1.echo(array); // Example7_4echoʾǰ

		ob2.bubbleSort(array); // 

		ob1.echo(array); // Example7_4echoʾź

	}

}

7-7 ArrayDemo4.java
public class ArrayDemo4 {

	public static void main(String[] args) {
		String[] hello = new String[3];

		for (int i = 0; i < hello.length; i++) {
			System.out.println(hello[i]);
		}
	}
}
޸ĺ
public class ArrayDemo4 {

	public static void main(String[] args) {
		String[] hello = new String[3];
		
		hello[0] = "Ϻ";
		hello[1] = "";
		hello[2] = "Ϻ";

		for (int i = 0; i < hello.length; i++) {
			System.out.println(hello[i]);
		}
	}
}

7-8 Student.javaArrayDemo5.java
public class Student {

	String name;
	char sex; // ԱMУFŮ
	int javaScore; // javaγ̳ɼ
	String grade; // ɼȼ

	public Student(String name, char sex, int javaScore) {
		this.name = name;
		this.sex = sex;
		this.javaScore = javaScore;
	}
	public int getJavaScore() {
		return javaScore;
	}
	public void setJavaScore(int javaScore) {
		this.javaScore = javaScore;
	}
	public void judgeGrade() {

		if (javaScore < 60) {
			grade = "!!!";
		} else if (javaScore < 85) {
			grade = "ϸ";
		} else {
			grade = "";
		}
	}

	public String getGrade() {
		judgeGrade();
		return grade;
	}
}

public class ArrayDemo5 {

	public static void main(String[] args) {
		Student[] stus = new Student[5];
		int i;
		int topScore;
		int topIndex;

		stus[0] = new Student("Jane", 'F', 82);
		stus[1] = new Student("Tom", 'M', 76);
		stus[2] = new Student("Jerry", 'M', 90);
		stus[3] = new Student("Chris", 'F', 95);
		stus[4] = new Student("John", 'M', 54);

		System.out.println("ѧϢ:");
		for (i = 0; i < stus.length; i++) {
			System.out.println(stus[i].name + "\t" + stus[i].sex + "\t"
					+ stus[i].javaScore);
		}

		// ҳɼõѧ
		topScore = stus[0].getJavaScore();
		topIndex = 0;
		for (i = 1; i < stus.length; i++) {
			if (topScore < stus[i].getJavaScore()) {
				topScore = stus[i].getJavaScore();
				topIndex = i;
			}
		}

		System.out.println("\nɼõѧ:");

		System.out.println(stus[topIndex].name + "   " + 
				stus[topIndex].sex + "   " + stus[topIndex].javaScore + 
"   " + stus[topIndex].getGrade());
		int totalScore = 0;

		// ѧƽɼ
		for (i = 0; i < stus.length; i++) {
			totalScore += stus[i].javaScore;
		}

		int ave = (int) ((double) totalScore / stus.length);
		System.out.println("\nѧƽɼ:   " + ave);
	}
}

7-9 ArrayDemo6.java
public class ArrayDemo6 {

	public static void main(String[] args) {

		int[][] a = { { 12, 34 }, { -5 }, { 3, 5, 7 } };

		int i, j;

		System.out.println("άaĳΪ" + a.length);

		for (i = 0; i < a.length; i++) {

			System.out.println("a[" + i + "]ĳΪ" + a[i].length);

			for (j = 0; j < a[i].length; j++) {

				System.out.print("a[" + i + "][" + j + "] = " 
+ a[i][j] + "\t");
			}
			System.out.println();
		}
	}
}

7-10 ArrayDemo7.java
public class ArrayDemo7 {

	public static void main(String[] args) {

		int[][] a = new int[3][];

		a[0] = new int[2];
		a[1] = new int[3];
		a[2] = new int[4];

		int i, j;
		for (i = 0; i < a.length; i++) {
			for (j = 0; j < a[i].length; j++) {
				a[i][j] = i + j;
				System.out.print("a[" + i + "][" + j + "] = " + a[i][j] + "\t");
			}
			System.out.println();
		}
	}
}

7-11 ArrayDemo8.java
public class ArrayDemo8 {

	public static void main(String[] args) {
		int[][] a = { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 }, { 10, 11, 12 } };

		int[][] b = new int[3][4];
		int i, j;

		System.out.println("aԪصֵΪ");

		for (i = 0; i < a.length; i++) {
			for (j = 0; j < a[i].length; j++) {
				System.out.print(a[i][j] + "\t");
			}
			System.out.println();
		}

		for (i = 0; i < a.length; i++) {
			for (j = 0; j < a[i].length; j++) {
				b[j][i] = a[i][j];
			}
		}

		System.out.println("תԺbԪصֵΪ");
		for (i = 0; i < b.length; i++) {
			for (j = 0; j < b[i].length; j++) {
				System.out.print(b[i][j] + "\t");
			}
			System.out.println();
		}
	}
}

7-12 ArraysDemo.java
import java.util.Arrays;		//ȱ

public class ArraysDemo {

	public void testStringArray() {
		String[] s1 = { "Tom", "Jerry", "Mike" };
		String[] s2 = { "Tom", "Jerry", "Mike" };

		System.out.println("s1s2Ƿͬ" + Arrays.equals(s1, s2));

		Arrays.sort(s1); // s1
		System.out.println("s1s2Ƿͬ" + Arrays.equals(s1, s2));

		System.out.println("Jerrys1еλã" + Arrays.binarySearch(s1, "Jerry"));
		System.out.println("Jerrys2еλã" + Arrays.binarySearch(s2, "Jerry"));

	}

	public void testIntArray() {

		int[] a = new int[5];
		int[] b = new int[5];
		int[] c = { 3, 5, 7, 9 };

		Arrays.fill(a, 1); // 1a

		System.arraycopy(a, 0, b, 0, a.length);
		// Systemarraycopyʵ鿽
		// aԴbĿ
		// aΪ0λÿʼa.lengthԪbb0ʼ
		System.out.println("abǷͬ" + Arrays.equals(a, b));

		Arrays.sort(c); // cԪ

		System.out.println("a:" + Arrays.toString(a));
		System.out.println("b:" + Arrays.toString(b));
		System.out.println("c:" + Arrays.toString(c));
	}

	public static void main(String[] args) {
		ArraysDemo ob = new ArraysDemo();
		ob.testStringArray();
		ob.testIntArray();
	}
}

