Java学习
21/03/21
数组使用
package com.cheng.array;
public class Demo03 {
public static void main(String[] args) {
int[] array = {1,2,3,4,5};
//打印全部数组元素
for (int i = 0; i <array.length; i++) {
System.out.println(array[i]);
}
System.out.println("============");
//计算全部元素的和
int sum = 0;
for (int i = 0; i <array.length; i++) {
sum = sum + array[i];
}
System.out.println("sum="+sum);
System.out.println("============");
//判断出最大元素
int max = array[0];
for (int i = 1; i <array.length ; i++) {
if (array[i]>max){
max = array[i];
}
}
System.out.println("max="+max);
}
}
输出结果:
1
2
3
4
5
============
sum=15
============
max=5
进程完成,退出码 0
array.for 以及 反转数组
package com.cheng.array;
public class Demo04 {
public static void main(String[] args) {
int[] arrays = {1,2,3,4,5};
//没有下标
for (int array: arrays) { //arrays.for
System.out.println(array);
}
printArray(arrays);
int[] reverse = reverse(arrays);
printArray(reverse);
}
//反转数组
public static int[] reverse(int[] arrays){
int[] result = new int[arrays.length];
for (int i = 0,j = arrays.length - 1; i < arrays.length; i++,j--) {
result[j] = arrays[i];
}
return result;
}
//打印数组元素
public static void printArray(int[] arrays){
for (int i = 0; i <arrays.length ; i++) {
System.out.print(arrays[i]+" ");
}
}
}
输出结果:
1
2
3
4
5
1 2 3 4 5 5 4 3 2 1
进程完成,退出码 0
多维数组
-
多维数组可以看作是数组的数组
-
二维数组
int a[][] = new int[4][2];
-
多维数组的使用
package com.cheng.array; public class Deom05 { public static void main(String[] args) { /*[4][2] 1,2 array[0] 2,3 array[1] 3,4 array[2] 4,5 array[3] */ int[][] array ={{1,2},{2,3},{3,4},{4,5}}; System.out.println(array[0][0]); System.out.println("================"); // printArray(array[0]); for (int i = 0; i <array.length ; i++) { for (int j = 0; j <array[0].length; j++) { System.out.println(array[i][j]); } } System.out.println("================"); } public static void printArray(int[] arrays){ for (int i = 0; i <arrays.length ; i++) { System.out.print(arrays[i]+" "); } } } 输出结果: 1 ================ 1 2 2 3 3 4 4 5 ================
Arrays类的使用
package com.cheng.array; import java.util.Arrays; public class Demo06 { public static void main(String[] args) { int[] a = {1,5,4,6,3,2}; int[] a2 = {1,5,4,6,3,2}; System.out.println(Arrays.toString(a));//打印数组元素 System.out.println("================="); Arrays.equals(a,a2); //数组比较 System.out.println("a与a2比较结果为:"+Arrays.equals(a,a2)); System.out.println("================="); Arrays.sort(a); //数组排序:升序 System.out.println(Arrays.toString(a)); System.out.println("================="); Arrays.binarySearch(a,3); System.out.println(Arrays.binarySearch(a,3)); //有数组元素3,从下标0开始计数 System.out.println(Arrays.binarySearch(a,7)); //无数组元素7,从1开始计数,且无数组元素要加"-" System.out.println("================="); Arrays.fill(a,2,4,0); //数组下标从2到4的元素赋值为0 System.out.println(Arrays.toString(a)); System.out.println("================="); Arrays.fill(a,0); //数组元素全部赋值为0 System.out.println(Arrays.toString(a)); } } 输出结果: [1, 5, 4, 6, 3, 2] ================= a与a2比较结果为:true ================= [1, 2, 3, 4, 5, 6] ================= 2 -7 ================= [1, 2, 0, 0, 5, 6] ================= [0, 0, 0, 0, 0, 0] 进程完成,退出码 0
冒泡排序
package com.cheng.array; import java.util.Arrays; public class Demo07 { public static void main(String[] args) { int[] array = {2, 1, 5, 3, 6, 4}; int[] sort = sort(array); System.out.println(Arrays.toString(sort)); } public static int[] sort(int[] array){ int temp = 0; //临时变量 for (int i = 0; i < array.length - 1; i++) { //外层循环,判断我们要走多少次 boolean flag = false; //优化排序,省略步骤,提高效率 for (int j = 0; j < array.length - 1 - i; j++) { //内层循环,判断比较两个数,如果第一个数比第二个数大(小),则交换两个数 if (array[j + 1] < array[j]) { temp = array[j]; array[j] = array[j + 1]; array[j + 1] = temp; flag = true; } } if (flag==false){ break; } } return array; } } 输出结果: [1, 2, 3, 4, 5, 6] 进程完成,退出码 0
稀疏数组
- 当一个数组中大部分元素为0(或者为同一元素时),可以使用稀疏数组来保存该数组。
- 稀疏数组的处理方式是:
- 记录数组一共有几行几列,有多少个不同的值
- 把具有不同值的元素和行列记录在一个小规模数组中,从而缩小程序的规模
- 如下:左边是原始数组,右边是稀疏数组
public class Demo08 {
public static void main(String[] args) {
//1.创建一个二维数组 11*11 0:没有棋子 1:黑棋 2:白棋
int[][] array1 = new int[11][11];
array1[1][2] = 1;
array1[2][3] = 2;
//输出原始数组
System.out.println("输出原始的数组");
for (int[] ints : array1) {
for (int anInt : ints) {
System.out.print(anInt+"\t");
}
System.out.println( );
}
System.out.println("=========================");
//转换为稀疏数组
//获取有效值的个数
int sum = 0;
for (int i = 0; i <11 ; i++) {
for (int j = 0; j <11 ; j++) {
if (array1[i][j]!=0){
sum++;
}
}
}
System.out.println("有效值的个数:"+sum);
//2.创建一个有效数组的稀疏数组
int[][] array2 = new int[sum+1][3];
array2[0][0]=11;
array2[0][1]=11;
array2[0][2]=sum;
//遍历二维数组,将非零的值,存放在稀疏数组中
int count=0;
for (int i = 0; i <array1.length; i++) {
for (int j = 0; j <array1[i].length; j++) {
if (array1[i][j]!=0){
count++;
array2[count][0]=i;
array2[count][1]=j;
array2[count][2]=array1[i][j];
}
}
}
//输出稀疏数组
System.out.println("稀疏数组");
for (int i = 0; i <array2.length; i++) {
System.out.println(array2[i][0]+"\t"
+array2[i][1]+"\t"
+array2[i][2]+"\t");
}
System.out.println("===========================");
System.out.println("还原稀疏数组");
//1.读取稀疏数组的值
int[][] array3 = new int[array2[0][0]][array2[0][1]];
//2.给其中的元素还原它的值
for (int i = 1; i <array2.length; i++) {
array3[array2[i][0]][array2[i][1]] = array2[i][2];
}
//3.打印
System.out.println("还原是数组");
for (int[] ints : array3) {
for (int anInt : ints) {
System.out.print(anInt+"\t");
}
System.out.println( );
}
}
}
输出结果:
输出原始的数组
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 0 0
=========================
有效值的个数:2
稀疏数组
11 11 2
1 2 1
2 3 2
===========================
还原稀疏数组
还原是数组
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 0 0
进程完成,退出码 0