七、數組的定義與使用
25 數組的基本定義
數組:一組相關變量的集合
引用資料類型
數組定義
1、數組動态初始化(預設值是對應類型的預設值)
資料類型 數組名稱 [] = new 資料類型[長度];
資料類型[] 數組名稱 = new 資料類型[長度];
2、數組靜态初始化
簡寫:
資料類型 數組名稱 [] = {資料1, 資料2, 資料3…};
完整格式(推薦):
資料類型 數組名稱 [] = new 資料類型[] {資料1, 資料2, 資料3…};
下标通路元素,下标從0開始
下标範圍:0 ~ 長度-1
// 1、動态初始化
int data[] = new int[3];
// 設定值
data[0] = 1;
data[1] = 2;
data[2] = 3;
//周遊,擷取值
for (int i = 0; i < data.length ; i++) {
System.out.println(data[i]);
// 1 2 3
}
// 2、靜态初始化
int[] data = new int[]{1, 2, 3};
//周遊,擷取值
for (int i = 0; i < data.length ; i++) {
System.out.println(data[i]);
// 1 2 3
}
26 數組引用傳遞分析
數組是引用資料類型
int[] data = new int[]{1, 2, 3};
int[] temp = data;
temp[0] = 10;
//周遊,擷取值
for (int i = 0; i < data.length ; i++) {
System.out.println(data[i]);
// 10 2 3
}
27 foreach輸出
JDK>=1.5 避免操作下标,從.NET引入
文法
foreach(資料類型 變量: 數組|集合){}
int[] data = new int[]{1, 2, 3};
for (int temp : data) {
System.out.println(temp);
// 1 2 3
}
28 二維數組
定義
1、動态初始化
資料類型[][] 數組名稱 = new 資料類型[行個數][列個數] ;
2、靜态初始化
資料類型[][] 數組名稱 = new 資料類型[][] {{資料1, 資料2, ...}, {資料1, 資料2, ...}...} ;
int[][] data = new int[][]{{1, 2, 3}, {4, 5, 6}};
// for循環
for (int x = 0 ; x < data.length ; x ++) {
for (int y = 0 ; y < data[x].length ; y ++) {
System.out.println(data[x][y]) ;
// 1 2 3 4 5 6
}
}
// foreach 循環
for (int[] row : data) {
for (int cell : row) {
System.out.println(cell) ;
// 1 2 3 4 5 6
}
}
29 數組與方法
同一塊堆記憶體被不同棧變量指向
class ArrayDemo{
public static int[] getArray(){
int[] arr = new int[] {1, 2, 3};
return arr;
}
public static void printArray(int[] arr){
for(int x : arr) {
System.out.println(x);
}
}
public static void main(String[] args) {
int[] arr = getArray();
printArray(arr);
// 1 2 3
}
}
主類:主方法所在的類
主類的功能不能過于複雜
// 封裝起來的數組工具類
class ArrayUtil{
public static int getSum(int[] arr){
int sum = 0 ;
for(int x : arr){
sum += x ;
}
return sum ;
}
public static double getAvg(int[] arr){
return getSum(arr) / arr.length;
}
public static int getMax(int[] arr){
// 假設最大值為第一個元素
int max = arr[0];
for(int x : arr){
if(x > max){
max = x;
}
}
return max;
}
public static int getMin(int[] arr){
// 假設最小值為第一個元素
int min = arr[0];
for(int x : arr){
if(x < min){
min = x;
}
}
return min;
}
public static void main(String[] args) {
int[] arr = new int[] {1, 2, 3, 4, 5};
System.out.println("sum: " + ArrayUtil.getSum(arr));
System.out.println("avg: " + ArrayUtil.getAvg(arr));
System.out.println("max: " + ArrayUtil.getMax(arr));
System.out.println("min: " + ArrayUtil.getMin(arr));
/**
sum: 15
avg: 3.0
max: 5
min: 1
*/
}
}
30 數組排序案例分析
類中沒有資料,可以使用static 靜态方法,直接使用類名調用
// 封裝起來的數組工具類
class ArrayUtil{
public static void sort(int[] arr){
// 第一層 控制比較次數 length - 1
for(int y = 0; y < arr.length - 1; y++){
// 第二層 無序區做比較即可 length - n - 1
for(int x = 0 ; x < arr.length - y - 1; x++){
if(arr[x] > arr[x+1]){
int temp = arr[x];
arr[x] = arr[x + 1];
arr[x+1] = temp;
}
}
}
}
public static void printArray(int[] arr){
System.out.print("[ ");
for(int x : arr){
System.out.print(x);
System.out.print(" ");
}
System.out.println("]");
}
public static void main(String[] args) {
int[] arr = new int[] {4, 5, 1, 3, 2};
ArrayUtil.sort(arr) ;
ArrayUtil.printArray(arr);
// [ 1 2 3 4 5 ]
}
}
31 數組轉置案例分析
方式一:
使用臨時數組,增加了垃圾
public static int[] reverse(int[] arr){
int[] temp = new int[arr.length] ;
int foot = arr.length - 1 ;
for(int i = 0; i < arr.length ; i++){
temp[foot--] = arr[i];
}
return temp;
}
public static void main(String[] args) {
int[] arr = new int[] {1, 2, 3, 4, 5};
arr = ArrayUtil.reverse(arr) ;
ArrayUtil.printArray(arr);
// [ 5 4 3 2 1 ]
}
java中整數相除,向下取整
System.out.println(5/3); // 1
System.out.println(5.0/3); // 1.66
方式二:
使用數組自身交換,使用if,增加了時間複雜度
public static void reverse(int[] arr){
int center = arr.length / 2;
int head = 0;
int tail = arr.length - 1;
for(int i = 0; i < center ; i++){
int temp = arr[head];
arr[head] = arr[tail];
arr[tail] = temp;
head ++;
tail --;
}
}
public static void main(String[] args) {
int[] arr = new int[] {1, 2, 3, 4, 5};
ArrayUtil.reverse(arr) ;
ArrayUtil.printArray(arr);
// [ 5 4 3 2 1 ]
}
32 數組相關類庫
1、數組排序
java.util.Arrays.sort(數組)
int[] arr = new int[] {5, 4, 2, 3, 1};
java.util.Arrays.sort(arr);
ArrayUtil.printArray(arr);
// [ 1 2 3 4 5 ]
2、數組拷貝
System.arraycopy(源數組, 源數組開始點, 目标數組, 目标數組開始點, 拷貝長度)
int[] arr1 = new int[] {1, 2, 3, 4, 5};
int[] arr2 = new int[] {11, 22, 33, 44, 55};
System.arraycopy(arr1, 1, arr2, 1, 3) ;
ArrayUtil.printArray(arr2);
// [ 11 2 3 4 55 ]
自定義方法實作
public static void arraycopy(int[] source, int sourceIndex, int[] target, int targetIndex, int length){
for(int i = 0 ; i < length; i++){
target[targetIndex + i] = source[sourceIndex + i];
}
}
public static void main(String[] args) {
int[] arr1 = new int[] {1, 2, 3, 4, 5};
int[] arr2 = new int[] {11, 22, 33, 44, 55};
ArrayUtil.arraycopy(arr1, 1, arr2, 1, 3) ;
ArrayUtil.printArray(arr2);
// [ 11 2 3 4 55 ]
}
33 方法可變參數
JDK >= 1.5
// 接收可變參數
public static int getSum(int ... arr){
int sum = 0 ;
for(int x : arr){
sum += x ;
}
return sum ;
}
public static void main(String[] args) {
int sum1 = ArrayUtil.getSum(1, 2, 3, 4, 5);
int sum2 = ArrayUtil.getSum(new int[] {1, 2, 3, 4, 5});
System.out.println(sum1); // 15
System.out.println(sum1); // 15
}
34 對象數組
class Person{
private String name;
private int age;
public Person(String name, int age){
this.name = name;
this.age = age;
}
public String getInfo(){
return "name: " + this.name +
" age: " + this.age ;
}
動态初始化
Person[] persons = new Person[3];
persons[0] = new Person("張三", 23) ;
persons[1] = new Person("李四", 24) ;
persons[2] = new Person("王五", 25) ;
for (Person person : persons) {
System.out.println(person.getInfo());
}
/**
name: 張三 age: 23
name: 李四 age: 24
name: 王五 age: 25
*/
靜态初始化
Person[] persons = new Person[]{
new Person("張三", 23),
new Person("李四", 24),
new Person("王五", 25)
};
for (Person person : persons) {
System.out.println(person.getInfo());
}
/**
name: 張三 age: 23
name: 李四 age: 24
name: 王五 age: 25
*/
數組特征:
1、數組缺點:長度固定
2、數組優勢:線性儲存,根據索引取值,速度較快,時間複雜度為1
八、引用傳遞實際應用
35 類關聯結構
class Car{
private String name;
private double price;
private Person person;
public Car(String name, double price){
this.name = name;
this.price = price;
}
// 設定、擷取車主
public void setPerson(Person person){
this.person = person ;
}
public Person getPerson(){
return this.person;
}
public String getInfo(){
return "name: " + this.name +
" price: " + this.price ;
}
}
class Person{
private String name;
private int age;
private Car car;
public Person(String name, int age){
this.name = name;
this.age = age;
}
public String getInfo(){
return "name: " + this.name +
" age: " + this.age ;
}
// 設定、擷取車輛
public void setCar(Car car){
this.car = car ;
}
public Car getCar(){
return this.car ;
}
public static void main(String[] args) {
Person person = new Person("張三", 23) ;
Car car = new Car("特斯拉", 24) ;
// 關聯車輛和人
person.setCar(car) ;
car.setPerson(person) ;
// 通過人擷取車輛
System.out.println( person.getCar().getInfo()) ;
// name: 特斯拉 price: 24.0
// 通過車輛擷取人
System.out.println( car.getPerson().getInfo()) ;
// name: 張三 age: 23
}
}
36 自身關聯
class Car{
private String name;
private double price;
private Person person;
public Car(String name, double price){
this.name = name;
this.price = price;
}
// 設定、擷取車主
public void setPerson(Person person){
this.person = person ;
}
public Person getPerson(){
return this.person;
}
public String getInfo(){
return "name: " + this.name +
" price: " + this.price ;
}
}
class Person{
private String name;
private int age;
private Car car;
private Person[] children ;
public Person(String name, int age){
this.name = name;
this.age = age;
}
// 增加孩子數組
public Person[] getChildren(){
return this.children;
}
public void setChildren(Person[] children){
this.children = children ;
}
public String getInfo(){
return "name: " + this.name +
" age: " + this.age ;
}
// 設定、擷取車輛
public void setCar(Car car){
this.car = car ;
}
public Car getCar(){
return this.car ;
}
public static void main(String[] args) {
Person person = new Person("張三", 23) ;
// 添加孩子
Person childA = new Person("小張", 19) ;
Person childB = new Person("小王", 19) ;
// 給孩子買車
childA.setCar(new Car("法拉利", 2000000.00)) ;
childB.setCar(new Car("賓利", 8000000.00)) ;
person.setChildren(new Person[] {
childA,
childB
}) ;
System.out.println(person.getInfo()) ;
for(Person child : person.getChildren()){
System.out.println( "\t|" + child.getInfo()) ;
System.out.println( "\t\t|" + child.getCar().getInfo()) ;
}
/**
name: 張三 age: 23
|name: 小張 age: 19
|name: 法拉利 price: 2000000.0
|name: 小王 age: 19
|name: 賓利 price: 8000000.0
*/
}
}
37 合成設計模式
拆分+組合
class 電腦{
private 主機 對象;
private 顯示器 對象數組;
}
class 顯示器{}
class 主機{
private CPU 對象;
private 鍵盤 對象;
private 鍵盤 對象;
}
class CPU {}
class 鍵盤 {}
class 鍵盤 {}
38 綜合實戰:資料表與簡單Java類映射轉換
資料表與簡單Java類之間基本映射關系
資料實體表設計 = 類的定義
表中的字段 = 類的成員屬性
表的一行記錄 = 類的一個執行個體化對象
表的多行記錄 = 對象數組
表的外鍵關聯 = 引用關聯
雇員和部門關系
一個部門對應多個雇員
一個雇員對應一個部門
定義階段:
定義實體類
配置關聯字段
開發階段:
定義對象
關聯關系
根據關系擷取資料
39 綜合實戰:一對多映射
班級對學生
40 綜合實戰:多對多映射
商品和顧客
41 綜合實戰:複雜多對多映射
使用者 權限