一、数组的定义与声明
数组:存放相同数据类型的一个容器,容器中的每一个内容称之为元素(变量)。
实例化方式:
【一维数组】:
动态初始化:
数据类型[] 数组名 = new 数据类型[数组长度];
数据类型 数组名[] = new 数据类型[数组长度];
静态初始化:
完全格式:数据类型[] 数组名 = new 数据类型[]{元素1,元素2,元素3,元素…,元素n};
简便格式:数据类型[] 数组名 = {元素1,元素2,元素3,元素…,元素n};
【二维数组】:
动态初始化:
数据类型[][] 数组名 = new 数据类型[m][n];
m:二维数组的长度 n:一维数组的长度
注意:m的长度必须有,n的长度可以有
静态初始化
完全格式:数据类型[][] 数组名 = new int[][]{{元素…},{元素…},{元素…}};
简化格式:数据类型[][] 数组名 = {{元素…},{元素…},{元素…}};
注意:简化格式不可以拆分开
即: int[][] a;
a = {{....},{.......}}
二、数组的扩容
【1】使用System.arraycopy(src, srcPos, dest, destPos, length);方法进行扩容
src:原来的数组
srcPos:需要拷贝的原来数组的起始索引
dest:需要拷贝到的数组
destPos:需要拷贝到数组的起始索引
length:要复制的数组元素的数量。
注意:
srcPos+length
小于src.length
destPos+length
小于dest.length
public static void main(String[] args) {
int[] a = {2,5,9,8,6,10};
int[] arr = new int[a.length*2];
System.arraycopy(a, 0, arr, 0, a.length);
a = arr;
for (int i = 0; i < a.length; i++) {
System.out.print(a[i]+" ");
}
}
【2】使用Arrays.copyOf(original, newLength)方法进行扩容
original:目标数组
newLength:需要的长度
public static void main(String[] args) {
int[] a = {2,5,9,8,6,10};
a = Arrays.copyOf(a, a.length*2);
for (int i = 0; i < a.length; i++) {
System.out.print(a[i]+" ");
}
}
三、数组的反序
【方法一】:
public static void main(String[] args) {
int[] a = {2,5,9,8,6,10};
//用于交换
int temp;
for (int i = 0; i < a.length/2; i++) {
temp = a[i];
a[i] = a[a.length-1-i];
a[a.length-1-i] = temp;
}
for (int i = 0; i < a.length; i++) {
System.out.print(a[i]+" ");
}
}
【方法二】:
public static void main(String[] args) {
int[] a = {2,5,9,8,6,10};
//用于交换
int temp;
int maxl = a.length - 1;
int minl = 0;
for (; minl < maxl; minl++ , maxl--) {
temp = a[minl];
a[minl] = a[maxl];
a[maxl] = temp;
}
for (int i = 0; i < a.length; i++) {
System.out.print(a[i]+" ");
}
}
四、数组的排序与二分法
【冒泡排序】:
public class BubbleSort {
public static void main(String[] args) {
int[] a = {9,5,1,15,4};
int temp;
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]) {
temp = a[j];
a[j] = a[j+1];
a[j+1] = temp;
}
}
}
for (int i : a) {
System.out.print(i+"\t");
}
}
}
【选择排序】:
public class SelectSort {
public static void main(String[] args) {
int[] a = {9,5,1,15,4};
int temp;
for (int i = 0; i < a.length; i++) {
for(int j = i+1; j < a.length; j++) {
if(a[i] > a[j]) {
temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
}
for (int i : a) {
System.out.print(i+"\t");
}
}
}
【快速排序】:
import java.util.Scanner;
public class QuickSort {
public static void main(String args[]){
System.out.print("请输入9个数字");
Scanner scan=new Scanner(System.in);
int a[]=new int[11];
for(int i=1;i<10;i++){
a[i]=scan.nextInt();
}
System.out.print("\n");
quick(a,1,9);
for(int i=1;i<10;i++){
System.out.print(a[i]+"\t");
}
System.out.print("\n");
}
/*
* 进行每组的排序
* */
public static int Partition(int a[],int low,int high){
int[] a1=new int[10];
a1=a;
int key;
a1[0]=a[low];
key=a[low];
//当low小于high时执行
while(low<high){
while(low<high&&key<=a1[high]){
--high;
}
a1[low]=a1[high];
while(low<high&&key>=a1[low]){
++low;
}
a1[high]=a1[low];
}
a1[low]=a1[0];
return low;
}
public static void quick(int a[],int low,int high){
int key;
if(low<high){
key=Partition(a,low,high);
quick(a,low,key-1);
quick(a,key+1,high);
}
}
}
【二分法查找】:
前提条件:数组有序
public static void main(String[] args) {
int[] array = {1,5,9,14,16,20,37,51};
//测试的值
int key = 15 ;
//数组的长度最大值
int maxl = array.length;
//数组的长度最小值
int minl = 0;
int mid = (maxl + minl)/2;
boolean flag = true;
while(key != array[mid]){
if(array[mid] > key){
maxl = mid -1 ;
}else{
minl = mid +1 ;
}
if(maxl < minl){
flag = false;
System.out.println("该数不在数组中");
break;
}
mid = (maxl + minl)/2;
}
if(flag){
System.out.println("该数在数组中的索引为:"+mid);
}
}