多维数组
数组的数组,其中的每一个元素都是一个一维数组
int a[][] = new int[2][5]
// 一个两行五列的数组
--------------------------------------------------
public class ArrayDemo05 {
public static void main(String[] args) {
int[][] arr = {{1,2},{2,4},{4,5},{5,6}};
System.out.println(arr[1][1]);
for (int i = 0; i < arr.length; i++) {
for (int j = 0; j < arr[i].length; j++) {
System.out.println(arr[i][j]);
}
}
}
}
Arrays类
java.util.Arrays
- Print array
- sort
import java.util.Arrays;
public class ArrayDemo06 {
public static void main(String[] args) {
int[] arr = {1,244,5635,3,56};
// print array
System.out.println(Arrays.toString(arr));
// output: [1, 244, 5635, 3, 56]
// sort: ascending
Arrays.sort(arr);
System.out.println(Arrays.toString(arr));
// output: [1, 3, 56, 244, 5635]
Arrays.fill(arr,2,4,0);
// fill 2 to 4 -- 0
// output: [1, 3, 0, 0, 5635]
Arrays.fill(arr,0);
System.out.println(Arrays.toString(arr));
// output: [0, 0, 0, 0, 0]
}
}
冒泡排序
两层循环,外层冒泡轮数,里层依次比较
时间复杂度 O(n^2)
import java.util.Arrays;
public class ArrayDemo07 {
public static void main(String[] args) {
int[] a = {2,4,6,2,5,3,1,5,7};
int[] sort = sort(a);
System.out.println(Arrays.toString(sort));
}
// bubble sorting
// compare 2 adjacent numbers
// if first > second, exchange them
// next time can save one sorting
public static int[] sort(int[] arr){
int temp = 0;
// outer loop, how many time should run
for (int i = 0; i < arr.length-1; i++) {
// inner loop, if first > second, exchange
for (int j = 0; j < arr.length-1-i; j++) {
if (arr[j+1]>arr[j]){
temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
}
}
return arr;
}
}
// output: [7, 6, 5, 5, 4, 3, 2, 2, 1]
稀疏数组
一种数据结构
当一个数组中大部分元素为0,或为同一数值时,可以使用稀疏数组来保存该数组
处理方式
- 记录数组一共有几行几列,有多少个不同值
- 把具有不同值的元素和行列及值记录在一个小规模的数组中,从而缩小程序的规模
- 创建二维数组,具有稀疏的特性
- 创建稀疏数组
- 还原数组
package com.zepei.array;
public class ArrayDemo08 {
public static void main(String[] args) {
// 1. build a 2-d array 11*11 value:0,1,2
int[][] arr = new int[11][11];
arr[1][2] = 1;
arr[2][3] = 2;
System.out.println("original arr" + arr);
for(int[] ints:arr){
for(int i:ints){
System.out.print(i+"\t");
}
System.out.println();
}
// turn to sparse array
// count valid member
int count = 0;
for (int i = 0; i < 11; i++) {
for (int j = 0; j < 11; j++) {
if(arr[i][j]!=0){
count++;
}
}
}
System.out.println("valid numbers " + count);
// 2. build an array for sparse array
int[][] arr1 = new int[count+1][3];
arr1[0][0] = 11;
arr1[0][1] = 11;
arr1[0][2] = count;
// traverse 2-d array, put non-zero value into sparse array
int c = 0;
for (int i = 0; i < arr.length; i++) {
for (int j = 0; j < arr[i].length; j++) {
if (arr[i][j] != 0){
c++;
arr1[c][0] = i;
arr1[c][1] = j;
arr1[c][2] = arr[i][j];
}
}
}
// print sparse array
for (int i = 0; i < arr1.length; i++) {
System.out.println(arr1[i][0]+"\t"
+arr1[i][1]+"\t"
+arr1[i][2]+"\t");
}
// reduction sparse array
// 1. read sparse array
int[][]arr2 = new int[arr1[0][0]][arr1[0][1]];
// 2. reduct value
for (int i = 1; i < arr1.length; i++) {
arr2[arr1[i][0]][arr1[i][1]] = arr1[i][2];
}
System.out.println("reduction arr" + arr2);
for(int[] ints:arr2){
for(int i:ints){
System.out.print(i+"\t");
}
System.out.println();
}
}
}
output
original arr[[I@61bbe9ba
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
valid numbers 2
11 11 2
1 2 1
2 3 2
reduction arr[[I@610455d6
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