数组概述
相同类型数据的有序集合
数组声明和定义
Datatype[] arrayName;//首选,推荐
Datatype arrayname[];//也可以,不推荐
Datatype[] arrayName=new Datatype[arraySize];//定义数组长度,分配空间
给数组赋值
arrayName[0]=1;
arrayName[1]=2;
arrayName[2]=3;
arrayName[3]=4;
//练习1.计算所有元素的和
数组初始化
静态初始化
int[] a = {1,2,3,4,5,6,7,8};
动态初始化:默认初始化
int b = new int[10];
b[0]=10;
数组的基本特点
1.数组的长度是确定的,数组一旦创建,大小就不能改变
2.不允许出现混合类型
3.数组元素可以是任何数据类型,包括基本类型和引用类型
4.数组变量属于引用类型,可以看成是对象
数组使用
//练习1,遍历数组
package com.bowenxu.array;
public class Demo01 {
public static void main(String[] args) {
//数组练习1,遍历数组
int[] a = {1,2,3,4,5,6};
for (int i = 0; i < a.length; i++) {
System.out.println(a[i]);
}
}
}
//练习2,求所有元素的和
package com.bowenxu.array;
public class Demo02 {
public static void main(String[] args) {
//求所有元素的和
int[] a = {1,2,3,4,5,6};
if (a.length==0)
{
System.out.println("数组为空");
return;
}
int sum =0;
for (int i = 0; i < a.length; i++) {
sum+=a[i];
}
System.out.println("所有元素的和是" + sum);
}
}
//练习3,求最大值
package com.bowenxu.array;
public class Demo03 {
public static void main(String[] args) {
//求最大值
int[] a = {1,2,3,4,5,6};
if (a.length==0)
{
System.out.println("数组为空");
return;
}
int max=a[0];
for (int i = 0; i < a.length; i++) {
if (a[i]>max){
max=a[i];
}
}
System.out.println("数组的最大值为" + max);
}
}
//练习4,反转数组(使用函数方法实现)
package com.bowenxu.array;
import java.util.Arrays;
public class Demo04 {
public static void main(String[] args) {
int[] a ={1,2,3,4,5,6};
int[] b= revers(a);
System.out.println(Arrays.toString(b));
}
//反转数组
public static int[] revers(int[] a){
int[] resoult=new int[a.length];
//反转数据
for (int i = 0; i < a.length; i++) {
resoult[a.length-1-i]=a[i];
}
return resoult;
}
}
//练习5,冒泡排序
package com.bowenxu.array;
import java.util.Arrays;
public class Demo05 {
public static void main(String[] args) {
int[] a ={1,4,3,5,2,7,10};
bubbleSort(a);
System.out.println(Arrays.toString(a));
}
//冒泡排序
public static int[] bubbleSort(int[] a){
for (int i = 0; i < a.length-1; i++) {
for (int j = i+1; j < a.length; j++) {
if (a[j]<a[i]){
int temp=a[j];
a[j]=a[i];
a[i]=temp;
}
}
}
return a;
}
}
多维数组
二维数组
int[][] a={{1,2},{3,4},{5,6}}
Arrays类
Java.util.Arrays类
常用,Arrays.toString(a)
Arrays.Sort(a)
稀疏数组
package com.bowenxu.array;
import java.util.Arrays;
public class Demo06 {
public static void main(String[] args) {
int[][] a =new int[11][11];
a[1][3]=1;
a[2][4]=2;
a[10][10]=1;
//普通数组
System.out.println("普通数组==============");
for (int i = 0; i < a.length; i++) {
System.out.println(Arrays.toString(a[i]));
}
System.out.println("稀疏数组==============");
//稀疏数组
int[][] b=toSparseArray(a);
for (int i = 0; i < b.length; i++) {
System.out.println(Arrays.toString(b[i]));
}
System.out.println("还原数组==============");
int[][] c=deSparseArray(b);
for (int i = 0; i < c.length; i++) {
System.out.println(Arrays.toString(c[i]));
}
}
//正常数组压缩为稀疏数组
public static int[][] toSparseArray(int[][] a){
//1.计算原始数组中的非零值个数
int count = 0;
for (int i = 0; i < a.length; i++) {
for (int j = 0; j < a[0].length; j++) {
if (a[i][j]!=0)
{
count++;
}
}
}
//2.创建稀疏数组
int[][] b = new int[count+1][3];
b[0][0]= a.length;
b[0][1]=a[0].length;
b[0][2]=count;
//3.稀疏数组存值
int bIndex=0;
for (int i = 0; i < a.length; i++) {
for (int j = 0; j < a[0].length; j++) {
if (a[i][j]!=0)
{
bIndex++;
b[bIndex][0]=i;
b[bIndex][1]=j;
b[bIndex][2]=a[i][j];
}
}
}
//返回稀疏数组
return b;
}
//解压稀疏数组(还原数组)
public static int[][] deSparseArray(int[][] b){
//1.创建还原数组
int[][] a=new int[b[0][0]][b[0][1]];
//2.填充有效值
for (int i = 1; i < b[0][2]+1; i++) {
a[b[i][0]][b[i][1]]=b[i][2];
}
return a;
}
}
运行结果
普通数组==============
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1]
稀疏数组==============
[11, 11, 3]
[1, 3, 1]
[2, 4, 2]
[10, 10, 1]
还原数组==============
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1]
Process finished with exit code 0