数组就是同一种数据的集合
数组的定义
1 //数据类型 数组名[] = new 数据类型[大小] 2 int a[] = new a[5]; 3 4 //---- 5 int b[]; 6 b = new int[5] 7 8 //定义并初始化 9 int c[] = {1,2,3,4,5,6,7,8,9};
数组的引用
1 //定义数组,为5个空间 2 int arr[] = new int[5]; 3 4 //引用数组并赋值 5 arr[0] = 1; //第0个空间赋值为1 6 7 arr[1] = 2; //第1个空间赋值为2 8 9 arr[2] = 3; //第2个空间赋值为3 10 11 arr[3] = 4; //第3个空间赋值为4 12 13 arr[4] = 5; //第4个空间赋值为5
public class ArrDemo { public static void main(String[] args) { int[] arr = new int[]{89,56,47,23,63}; ergodic(arr); } } //遍历 public static void ergodic(int[] arr) { for(int i =0 ; i < arr.length; i++) { System.out.print("[" + i + "]" + "=" + arr[i] + " "); } System.out.println(); }
数组对象
实例化数组对象,但不能直接赋值,需要再次实例化Dog的对象,存放在数组对象里
1 package com.beekc.www; 2 import java.io.*; 3 4 public class Beekc { 5 public static void main(String[] args) { 6 //实例化空间为4的数组对象,但不能直接赋值,需要再次实例化Dog的对象,存放在数组对象里 7 Dog dogs[] = new Dog[4]; 8 9 InputStreamReader isr = new InputStreamReader(System.in); 10 BufferedReader br = new BufferedReader(isr); 11 try{ 12 for(int i =0 ; i < dogs.length ; i++) 13 { 14 //实例化狗对象 15 dogs[i] = new Dog(); 16 System.out.printf("请输入狗的名字:"); 17 //接收控制台数据 18 String name = br.readLine(); 19 //将名字给对象 20 dogs[i].setName(name); 21 22 System.out.printf("请输入狗的体重:"); 23 float weigth = Float.parseFloat(br.readLine()); 24 dogs[i].setWeigth(weigth); 25 } 26 27 //总体重变量 28 float allweigth = 0; 29 //累加所有对象体重 30 for(int i =0; i < dogs.length ; i++) 31 { 32 allweigth += dogs[i].getWeigth(); 33 } 34 35 System.out.printf("中体重为:" + allweigth); 36 37 }catch (Exception e) 38 { 39 40 } 41 42 } 43 } 44 45 //定义一个狗类 46 class Dog 47 { 48 private String name; 49 private float weigth; 50 51 public String getName() { 52 return name; 53 } 54 55 public void setName(String name) { 56 this.name = name; 57 } 58 59 public float getWeigth() { 60 return weigth; 61 } 62 63 public void setWeigth(float weigth) { 64 this.weigth = weigth; 65 } 66 }
定义多维数组
1 //数据类型 数组名[][] = new 数据类型[行数][列数] 2 int arr[][] = new int[3][3]; 3 4 //实例 5 package com.beekc.www; 6 7 public class Beekc { 8 public static void main(String[] args) { 9 10 int arr[][] = new int[3][3]; 11 12 for(int i = 0 ; i < 3 ; i++) 13 { 14 for(int j = 0 ; j < 3 ; j++) 15 { 16 arr[i][j] = 0; 17 } 18 } 19 20 arr[0][0] = 1; 21 arr[1][1] = 1; 22 arr[2][2] = 1; 23 //遍历二维数组 24 for(int i = 0 ; i < 3 ; i++) 25 { 26 for(int j = 0 ; j < 3 ; j++) 27 { 28 System.out.print(arr[i][j]); 29 } 30 System.out.print("\n"); 31 } 32 33 } 34 } 35 36 /*输出 37 100 38 010 39 001 40 */
查找
1 //取最大的值 2 public int getMax(int[] arr) 3 { 4 int max = arr[0]; 5 for (int x =1; x < arr.length; x++) 6 { 7 if(arr[x]>max) 8 { 9 max = arr[x]; 10 } 11 } 12 return max; 13 }
1 //取最大值的下标 2 public int getMaxIndex(int[] arr) 3 { 4 int maxIndex = 0; 5 for(int x = 1; x < arr.length; x++) 6 { 7 if(arr[x]>arr[0]) 8 { 9 maxIndex = x; 10 } 11 } 12 return maxIndex; 13 }
1 //查找 2 public int getIndex(int[] arr,int key) 3 { 4 for(int i = 0; i < arr.length; i++) 5 { 6 if(arr[i] == key) 7 { 8 return i; 9 } 10 } 11 return -1; 12 }
排序
//选择排序 public void selectSort(int[] arr) { for(int x = 0; x < arr.length-1; x++) { for(int y = x+1; y < arr.length;y++) { swap(arr,x,y); } } } //选择排序优化版 public void selectSort2(int[] arr) { for(int x = 0; x < arr.length-1; x++) { int num = arr[x]; int index = x; for(int y = x+1; y < arr.length; y++) { if(num > arr[y]) { num = arr[y]; index = y; } } if(index!=x) { swap(arr,x,index); } } }
1 //冒泡排序 2 public void bubbleSort(int[] arr) 3 { 4 for(int x = 0; x < arr.length-1; x++) 5 { 6 for (int y = 0 ; y < arr.length-1-x; y++) 7 { 8 swap(arr,y,y+1); 9 } 10 } 11 }
//空间交换 private static void swap(int[] arr, int x, int y) { if(arr[x]>arr[y]) { int temp = arr[x]; arr[x] = arr[y]; arr[y] = temp; } }