Java SE Lesson 4
1. 包裝類(Wrapper Class)。針對於原生數據類型的包裝。所有的包裝類(8個)都位於java.lang包下。Java中的8個包裝類分別是:Byte, Short, Integer, Long, Float, Double, Character, Boolean。他們的使用方式都是一樣的,可以實現原生數據類型與包裝類型的雙向轉換。
public class IntegerTest
{
public static void main(String[] args)
{
int a = 10;
Integer integer = new Integer(a);
int b = integer.intValue();
System.out.println(a == b);
}
}
2. 數組(Array):相同類型數據的集合就叫做數組。
3. 如何定義數組。 type[]變量名 = new type[數組中元素的個數];可以按照下列方式定義長度爲10的數組: int[] a = new int[10]; 或者 int a[] = new int[10];
4. 數組中的元素索引是從0開始的。對於數組來說,最大的索引==數組的長度– 1。
5. 定義數組的第3種方式: type[]變量名 = {new type[]}{逗號分隔的初始化值列表};
6. Java中的每個數組都有一個名爲length的屬性,表示數組的長度。length屬性是public,final,int的。數組長度一旦確定,就不能改變大小。
public class ArrayTest
{
public static void main(String[] args)
{
/*
int[] a = new int[4];
a[0] = 1;
a[1] = 2;
a[2] = 3;
a[3] = 4;
System.out.println(a[3]);
*/
/*
int a[] = new int[2];
a[0] = 1;
a[1] = 2;
System.out.println(a[1]);
*/
/*
int[] a = {1, 2, 3, 4};
System.out.println(a[2]);
int[] b = new int[]{1, 2, 3, 4};
System.out.println(b[3]);
*/
/*
int[] a = new int[100];
for(int i = 0; i < a.length; i++)
{
a[i] = i + 1;
}
for(int i = 0; i < a.length; i++)
{
System.out.println(a[i]);
}
//a.length = 200;
*/
/*
int[] a = new int[4];
System.out.println(a[0]);
boolean[] b = new boolean[3];
System.out.println(b[2]);
*/
int[] a = {1, 2 ,3};
int[] b = {1, 2, 3};
System.out.println(a.equals(b));
}
}
7. int[] a = new int[10],其中a是一個引用,它指向了生成的數組對象的首地址,數組中每個元素都是int類型,其中僅存放數據值本身。
public class ArrayTest2
{
public static void main(String[] args)
{
Person[] p = new Person[3];
p[0] = new Person(10);
p[1] = new Person(20);
p[2] = new Person(30);
for(int i = 0; i < p.length; i++)
{
System.out.println(p[i].age);
}
Person[] p2 = new Person[5];
for(int i = 0; i < p2.length; i++)
{
System.out.println(p2[i]);
}
}
}
class Person
{
int age;
public Person(int age)
{
this.age = age;
}
}
public class ArrayTest3
{
public static void main(String[] args)
{
Student[] s = new Student[100];
for(int i = 0; i < s.length; i++)
{
s[i] = new Student();
s[i].name = i % 2 == 0 ? "zhangsan" : "lisi";
}
for(int i = 0; i < s.length; i++)
{
System.out.println(s[i].name);
}
}
}
class Student
{
String name;
}
8. 二維數組。二維數組是一種平面的二維結構,本質上是數組的數組。二維數組的定義式:type[][] a = new type[2][3];
public class ArrayTest4
{
public static void main(String[] args)
{
int[][] a = new int[2][3];
//System.out.println(i[0] instanceof int[]);
int m = 0;
for(int i = 0; i < 2; i++)
{
for(int j = 0; j < 3; j++)
{
m++;
a[i][j] = 2 * m;
}
}
}
}
public class ArrayTest5
{
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[1];
*/
//int[][] a = new int[][3];
//int[] a = new int[]{1, 2 ,3};
int[][] a = new int[][]{ {1 ,2 ,3}, {4}, {5, 6, 7, 8} };
for(int i = 0; i < a.length; i++)
{
for(int j = 0; j < a[i].length; j++)
{
System.out.print(a[i][j] + " ");
}
System.out.println();
}
}
}
9.三維數組。type[][][] a = new type[2][3][4];
public class ThreeDimensionArrayTest
{
public static void main(String[] args)
{
int[][][] a = new int[2][3][4];
System.out.println(a instanceof int[][][]);
System.out.println(a[0] instanceof int[][]);
System.out.println(a[0][0] instanceof int[]);
for(int i = 0; i < a.length; i++)
{
for(int j = 0; j < a[i].length; j++)
{
for(int k = 0; k < a[i][j].length; k++)
{
a[i][j][k] = 100;
}
}
}
}
}
10.冒泡排序。(掌握交換排序,快速排序的原理與實現方式)
public class BubbleSortTest
{
public static void bubbleSort(int[] a)
{
for(int i=0;i<a.length-1;i++)
{
for(int j=0;j<a.length-1-i;j++)
{
if(a[j]>a[j+1])
{
int temp = a[j];
a[j] = a[j+1];
a[j+1] = temp;
}
}
}
for(int k=0;k<a.length;k++)
{
System.out.println(a[k]);
}
}
public static void main(String[] args)
{
int[] a = {4,6,3,8,7,9,2,1,0};
bubbleSort(a);
}
}
11. 二分查找(Binary Search):待查找的數組要有序。
public class ArraySearchTest
{
public static int search(int[] array, int value)
{
for(int i = 0; i < array.length; i++)
{
if(value == array[i])
{
return i;
}
}
return -1;
}
public static int binarySearch(int[] array, int value)
{
int low = 0;
int high = array.length - 1;
int middle;
while(low <= high)
{
middle = (low + high) / 2;
for(int i = 0; i < array.length; i++)
{
System.out.print(array[i]);
if(i == middle)
{
System.out.print("#");
}
System.out.print(" ");
}
System.out.println();
if(array[middle] == value)
{
return middle;
}
if(value < array[middle])
{
high = middle - 1;
}
if(value > array[middle])
{
low = middle + 1;
}
}
return -1;
}
public static void main(String[] args)
{
int[] a = new int[]{1, 2, 3, 4, 5, 6, 7, 8, 9};
int value = 11;
int index = search(a, value);
System.out.println(index);
System.out.println("-----------------");
int[] b = new int[]{1, 2, 3, 4, 5, 6, 7, 8, 9};
int index2 = binarySearch(b , 10);
System.out.println(index2);
}
}
12. 隨機生成50個數字(整數),每個數字的範圍是[10, 50],統計每個數字出現的次數以及出現次數最多的數字與它的個數,最後將每個數字及其出現次數打印出來,如果某個數字出現次數爲0,則不要打印它。打印時按照數字的升序排列。