1. 数组的定义:
1.1 存放一组数据类型相同的变量;
1.2 声明/定义/初始化:元素数据类型[] 数组名 = new 元素数据类型[数组长度];
1.3 数组名存放在栈中,数组本身是一个对象,数组对象本身存放在堆上。
1.3.1 如果只声明数组名时(int[] nums;),栈中存放了数组名,但是堆中没有数组对象;
1.3.2 只有实例化数组对象时(nums = {1, 2, 3}; nums = new int[5];),堆上开辟空间存放数组对象,数组名指向数组对象。
package com.langtao.array;
public class Demo2 {
public static void main(String[] args) {
int[] numbers = new int[10];
for (int i = 0; i < numbers.length; i++) {
numbers[i] = i + 1;
}
for (int i = 0; i < numbers.length; i++) {
System.out.println("numbers[" + i +"]=" +numbers[i]);
}
int[] array1 = {1, 2, 3};
Demo1[] d1 = {new Demo1(), new Demo1()};
}
}
2. 数组的使用
package com.langtao.array;
public class Demo3 {
public static void main(String[] args) {
int[] arrayOriginal = {1, 2, 3, 4, 5};
printArray(arrayOriginal);
System.out.println("==============");
int[] reverseOriginal = reverseArray(arrayOriginal);
printArray(reverseOriginal);
System.out.println("====In the main method call the reverse method====");
reverseArray1(arrayOriginal);
for (int i : arrayOriginal) {
System.out.println(i);
}
System.out.println("====In the main method call the reverse method====");
}
public static void printArray(int[] array){
for (int i = 0; i < array.length; i++) {
System.out.print(array[i] + " ");
}
}
public static int[] reverseArray(int[] array){
int[] reverseArray = new int[array.length];
for (int i = 0; i < reverseArray.length; i++) {
reverseArray[i] = array[reverseArray.length - i - 1];
}
return reverseArray;
}
public static void reverseArray1(int[] array){
int tmp = 0;
for (int i = 0; i < array.length/2; i++) {
tmp = array[i];
array[i] = array[array.length - i - 1];
array[array.length - i - 1] = tmp;
}
System.out.println("====In the method====");
for (int i : array) {
System.out.println(i);
}
System.out.println("====In the method====");
}
}
3.二维数组
package com.langtao.array;
public class Demo4 {
public static void main(String[] args) {
int[][] array2 = {{1,2}, {2,3}, {3,4}, {4,5}};
printArray(array2[0]);
System.out.println("==============");
printArray(array2);
}
public static void printArray(int[] array){
for (int i = 0; i < array.length; i++) {
System.out.print(array[i] + " ");
}
}
public static void printArray(int[][] array){
for (int i = 0; i < array.length; i++) {
for (int j = 0; j < array[i].length; j++) {
System.out.print(array[i][j] + " ");
}
System.out.println();
}
}
}
4. 冒泡排序
package com.langtao.array;
import java.util.Arrays;
public class Demo5 {
public static void main(String[] args) {
int[] a = {1, 3, 2, 5, -1, 23, 6};
int[] b = {9, 8, 7, 6, 5, 4, 3, 2, 1};
System.out.println(Arrays.toString(a));
// Arrays.sort(a);
// System.out.println(Arrays.toString(a));
sort1(a);
System.out.println(Arrays.toString(a));
sort1(b);
System.out.println(Arrays.toString(b));
}
public static void sort1(int[] array){
for (int i = 0; i < array.length - 1 ; i++) {
for (int j = 0; j < array.length - 1 - i; j++) {
if (array[j] > array[j + 1]){
int tmp = array[j];
array[j] = array[j + 1];
array[j + 1] = tmp;
}
}
}
}
}
5.稀疏数组
package com.langtao.array;
import java.util.Arrays;
/*
* The sparse array is used to compress the array which has only limited unequal values comparing to the majority ones.
* For example, the original array can be like this:
* 0 0 0 0 0
* 0 1 0 0 0
* 0 0 2 0 0
* 0 0 0 0 0
* 0 0 0 0 0
* 0 0 0 0 0
* We can compress this array into a sparse array as:
* 6 5 2
* 1 1 1
* 2 2 2
* In the first line of the array, we record the size and non-zero elements. Then in the second row, we record the first
* non-zero element's coordinates and value and in the next lines we record the same information.
* Then you can transform the sparse array back to the original array.
* */
public class Demo6 {
public static void main(String[] args) {
int[][] array = new int[6][5];
array[1][2] = 1;
array[2][3] = 2;
array[1][1] = 99;
printArray(array);
System.out.println("================");
int[][] sparseArray = sparseArray(array);
printArray(sparseArray);
System.out.println("****************");
int[][] originalArray = reverseSparseArray(sparseArray);
printArray(originalArray);
}
public static void printArray(int[][] array){
for (int i = 0; i < array.length; i++) {
for (int j = 0; j < array[i].length; j++) {
System.out.print(array[i][j] + " ");
}
System.out.println();
}
}
public static int[][] sparseArray(int[][] array){
//get the numbers of non-zero values in the original array.
int count = 0;
for (int i = 0; i < array.length; i++) {
for (int j = 0; j < array[i].length; j++) {
if(array[i][j] != 0){
count++;
}
}
}
/*
In the first row of the sparse array, it records the size of the original array and the
total number of the non-zero elements.
*/
int[][] sparseArray = new int[count + 1][3];
sparseArray[0][0] = array.length;
sparseArray[0][1] = array[0].length;
sparseArray[0][2] = count;
int index = 1;
for (int i = 0; i < array.length; i++) {
for (int j = 0; j < array[i].length; j++) {
if (array[i][j] != 0){
sparseArray[index][0] = i;
sparseArray[index][1] = j;
sparseArray[index][2] = array[i][j];
index++;
}
}
}
return sparseArray;
}
public static int[][] reverseSparseArray(int[][] array){
int row = array[0][0];
int colume = array[0][1];
int[][] reverseSparseArray = new int[row][colume];
for (int i = 1; i < array.length; i++) {
int realRow = array[i][0];
int realColume = array[i][1];
reverseSparseArray[realRow][realColume] = array[i][2];
}
return reverseSparseArray;
}
}