数组的四个基本特点:
- package org.psmaxwell.array;
-
- public class ArrayDemo01 {
- //变量的类型 变量的名字 = 变量的值;
- //数组类型
-
- public static void main(String[] args) {
- int[] nums; // 1.定义
-
- nums = new int[10]; //2.创建一个数组
-
- //3.给数组元素中赋值
- nums[0] = 1;
- nums[1] = 2;
- nums[2] = 3;
- nums[3] = 4;
- nums[4] = 5;
- nums[5] = 6;
- nums[6] = 7;
- nums[7] = 8;
- nums[8] = 9;
- nums[9] = 10;
-
- //计算所有元素的和
- int sum = 0;
- //获取数组长度: arrays.length
-
- for (int i = 0; i < nums.length; i++) {
- sum = sum + nums[i];
- }
- System.out.println("总和为:"+sum);
- }
- }
- 静态初始化:
- int[] a = {1, 2, 3};
- Man[] mans = {new Man(1,1),new Man(2,2)};
- 动态初始化:
- int[] a = new int[2];
- a[0]=1;
- a[1]=2;
数组的默认初始化:
数组是引用类型,它的元素相当于类的实例变量,因此数组一经分配空间,其中的每个元素也被按照实例变量同样的方式被隐式初始化
- package org.psmaxwell.array;
-
- public class ArrayDemo02 {
- public static void main(String[] args) {
- // 静态初始化: 创建 + 赋值
- int[] a = {1,2,3,4,5,6,7,8,9};
- System.out.println(a[0]);
-
- //动态初始化
- int[] b = new int[10];
- b[0] = 10;
-
- System.out.println(b[0]);
- System.out.println(b[1]);
- System.out.println(b[2]);
- System.out.println(b[3]);
- }
- }
- package org.psmaxwell.array;
-
- public class ArrayDemo02 {
- public static void main(String[] args) {
- // 静态初始化: 创建 + 赋值
- int[] a = {1,2,3,4,5,6,7,8,9};
- System.out.println(a[0]);
-
- for (int i = 0; i < a.length; i++) {
- System.out.println(a[i]);
- }
-
- //动态初始化: 包含默认初始化
- // int[] b = new int[10];
- // b[0] = 10;
- // b[1] = 10;
-
- // System.out.println(b[0]);
- // System.out.println(b[1]);
- // System.out.println(b[2]);
- // System.out.println(b[3]);
- }
- }
- package org.psmaxwell.array;
-
- public class ArrayDemo04 {
- public static void main(String[] args) {
- int[] arrays = {1,2,3,4,5};
-
- // //jdk 1.5 没有下标
- // for (int array : arrays) {
- // System.out.println(array);
- // }
-
- // printArray(arrays);
-
- int[] reverse = reverse(arrays);
- printArray(reverse);
- }
-
- // 打印数组元素
- public static void printArray(int[] arrays){
- for (int i = 0; i < arrays.length; i++) {
- System.out.print(arrays[i]+" ");
- }
- }
-
- //反转数组
-
- public static int[] reverse(int[] arrays) {
- int[] result = new int[arrays.length];
-
- //反转的操作
- for (int i = 0,j=result.length-1;i< arrays.length; i++,j--) {
- result[j] = arrays[i];
- }
-
- return result;
- }
- }
- package org.psmaxwell.array;
-
- public class ArrayDemo05 {
- 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}};
- for (int i = 0; i < array.length; i++) {
- for (int j = 0; j < array[i].length; j++) {
- System.out.println(array[i][j]);
- }
- }
-
- // System.out.println(array[0][0]);
- // System.out.println(array[0][1]);
- // System.out.println(array[1][0]);
- // System.out.println(array[1][1]);
- // System.out.println(array[2][0]);
- // System.out.println(array[2][1]);
- // System.out.println(array[3][0]);
- // System.out.println(array[3][1]);
- }
-
- // 打印数组元素
- public static void printArray(int[] arrays){
- for (int i = 0; i < arrays.length; i++) {
- System.out.print(arrays[i]+" ");
- }
- }
- }
- package org.psmaxwell.array;
-
- import java.util.Arrays;
-
- public class ArrayDemo06 {
- public static void main(String[] args) {
-
- int[] a = {1,2,3,4,9000,1123,231,42,633,67,74};
-
- // System.out.println(a); //[I@e9e54c2
-
- //打印数组元素Arrays.toString
- // System.out.println(Arrays.toString(a));
- // printArray(a);
-
- Arrays.sort(a); //数组进行自动排序
- System.out.println(Arrays.toString(a));
- }
-
- public static void printArray(int[] a){
- for (int i = 0; i < a.length; i++) {
- if(i==0){
- System.out.print("[");
- }
- if(i== a.length-1){
- System.out.println(a[i]+"]");
- }else{
- System.out.print(a[i]+", ");
- }
-
- }
- }
- }
- package org.psmaxwell.array;
-
- import java.util.Arrays;
-
- public class ArrayDemo06 {
- public static void main(String[] args) {
-
- int[] a = {1,2,3,4,9000,1123,231,42,633,67,74};
-
- // System.out.println(a); //[I@e9e54c2
-
- //打印数组元素Arrays.toString
- // System.out.println(Arrays.toString(a));
- // printArray(a);
-
- Arrays.sort(a); //数组进行自动排序
- System.out.println(Arrays.toString(a));
-
- }
-
- public static void printArray(int[] a){
- for (int i = 0; i < a.length; i++) {
- if(i==0){
- System.out.print("[");
- }
- if(i== a.length-1){
- System.out.println(a[i]+"]");
- }else{
- System.out.print(a[i]+", ");
- }
-
- }
- }
- }
- package org.psmaxwell.array;
-
- import java.util.Arrays;
-
- public class ArrayDemo07 {
- public static void main(String[] args) {
- int[] a = {1,3,5,6,7,3,8,9,22,32,21,88};
- int[] sort = sort(a); //调用完我们自己写的排序方法以后,返回一个排序后数组
-
- System.out.println(Arrays.toString(sort));
-
- }
- // 冒泡排序
- //1.比较数组中,两个相邻的元素,如果第一个数比第二个数大,我们就交换他们的位置
- //2.每一次比较,都会产生一个最大,或者最小数字
- //3.下一轮则可以少一次排序
- //4.依次循环,直到结束。
-
- public static int[] sort(int[] array){
- //临时变量
- int temp = 0;
- //外层循环,判定我们这个要走多少次?
- for (int i = 0; i < array.length-1; i++) {
- boolean flag = false; // 通过flag标识位减少没有意义的比较 优化
-
- //内层循环,比价判定两个数,如果第一个数,比第二个数大,则交换位置。
- 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;
-
- }
- }
- package org.psmaxwell.array;
-
- public class ArrayDemo08 {
- public static void main(String[] args) {
- //1.创建一个二维数组 11*11 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();
- }