Mastering the art of Java arrays: the advanced path from beginner to master (2)

⭐ Multidimensional arrays

A multidimensional array can be viewed as an array of arrays. There can be two-dimensional, three-dimensional, or even more dimensional arrays, but they are rarely used in actual development. Up to a two-dimensional array.
insert image description here

[eg] Declaration of two-dimensional array

	public class Test {
		public static void main(String[ ] args) {
		// Java中多维数组的声明和初始化应按从低维到高维的顺序进行
		int[ ][ ] a = new int[3][ ];
		a[0] = new int[2];
		a[1] = new int[4];
		a[2] = new int[3];
		// int a1[ ][ ]=new int[ ][4];//非法
	}
}

[eg] Static initialization of two-dimensional array

	public class Test {
		public static void main(String[ ] args) {
			int[ ][ ] a = { { 1, 2, 3 }, { 3, 4 }, { 3, 5, 6, 7 } };
			System.out.println(a[2][3]);
	}
}

insert image description here

[eg] Dynamic initialization of two-dimensional array

import java.util.Arrays;
		public class Test {
			public static void main(String[ ] args) {
				int[ ][ ] a = new int[3][ ];
				// a[0] = {1,2,5}; //错误,没有声明类型就初始化
				a[0] = new int[ ] { 1, 2 };
				a[1] = new int[ ] { 2, 2 };
				a[2] = new int[ ] { 2, 2, 3, 4 };
		System.out.println(a[2][3]);
		System.out.println(Arrays.toString(a[0]));
		System.out.println(Arrays.toString(a[1]));
		System.out.println(Arrays.toString(a[2]));
	}
}

The execution result is as follows:
insert image description here

⭐ Arrays store table data

Tables are the most ubiquitous model in the computing world. All the data you see on the Internet are essentially "tables".
insert image description here
We observe the table and find that each row can be stored using a one-dimensional array:

Object[ ] a1 = {1001,"高淇",18,"讲师","2-14"};
Object[ ] a2 = {1002,"高小七",19,"助教","10-10"};
Object[ ] a3 = {1003,"高小琴",20,"班主任","5-5"};

Notice:

⭐	此处基本数据类型”1001”,本质不是 Object 对象。JAVA 编译器会自动把基本数据类型“自动装箱”成包装类对象。

In this way, we only need to define another two-dimensional array, and put the above three arrays into it:

Object[ ][ ] emps = new Object[3][ ];
emps[0] = a1;
emps[1] = a2;
emps[2] = a3;

[eg] Use a two-dimensional array to save table data

import java.util.Arrays;
		public class Test {
			public static void main(String[ ] args) {
				Object[ ] a1 = {1001,"高淇",18,"讲师","2-14"};
				Object[ ] a2 = {1002,"高小七",19,"助教","10-10"};
				Object[ ] a3 = {1003,"高小琴",20,"班主任","5-5"};
				Object[ ][ ] emps = new Object[3][ ];
				emps[0] = a1;
				emps[1] = a2;
				emps[2] = a3;
		System.out.println(Arrays.toString(emps[0]));
		System.out.println(Arrays.toString(emps[1]));
		System.out.println(Arrays.toString(emps[2]));
	}
}

[eg] Use javabean and one-dimensional array to save table information
insert image description here

import java.util.Arrays;
		public class Test {
		 	public static void main(String[ ] args) {
 				Emp[] emps = {
 				new Emp(1001,"高淇",18,"讲师","2-14"),
 				new Emp(1002,"高小七",19,"助教","10-10"),
				new Emp(1003,"高小八",20,"班主任","5-5")
	 };
				 for (Emp e:emps){
			 System.out.println(e);
		 }
	 }
}
class Emp {
 	private int id;
 	private String name;
 	private int age;
 	private String job;
 	private String hiredate;
 		public Emp(int id, String name, int age, String job, String hiredate) {
 			this.id = id;
 			this.name = name;
 			this.age = age;
 			this.job = job;
 			this.hiredate = hiredate;
 	}
 @Override
 		public String toString() {
 			return "["+id+","+name+","+age+","+job+","+hiredate+"]";
 }
 		public int getId() {
			 return id;
 }
 		public void setId(int id) {
 			this.id = id;
 }
 		public String getName() {
 			return name;
 }
 		public void setName(String name) {
 			this.name = name;
 }
 		public int getAge() {
 			return age;
 }
 		public void setAge(int age) {
 			this.age = age;
 }
 		public String getJob() {
 			return job;
 }
 		public void setJob(String job) {
 			this.job = job;
 }
 		public String getHiredate() {
		 	return hiredate;
 }
 		public void setHiredate(String hiredate) {
 			this.hiredate = hiredate;
 	}
}

⭐Comparable interface

insert image description here

To compare multiple objects, it is necessary to have "comparison rules" and then implement sorting.

In fact, the bottom layer of the sorting algorithm in java also relies on the Comparable interface.

There is only one method in the Comparable interface:
public int compareTo(Object obj) obj is the object to be compared

In the method, compare the current object with the object obj. If it is greater than 1, it returns 0, and if it is less than it returns -1. (1 here can also be a positive integer, and -1 can also be a negative integer). The code of the compareTo method is also relatively fixed:

	public int compareTo(Object o) {
			Man man = (Man) o;
			if (this.age < man.age) {
				return -1;
	}
			if (this.age > man.age) {
				return 1;
	}
				return 0;
}

[Test Comparable Interface] Use the Arrays class to sort array elements 2

import java.util.Arrays;
		public class Test {
			public static void main(String[ ] args) {
				Man[ ] msMans = { new Man(3, "a"), new Man(60, "b"), new Man(2, "c") };
				Arrays.sort(msMans);
			System.out.println(Arrays.toString(msMans));
	}
}
		class Man implements Comparable {
			int age;
			int id;
			String name;
		public Man(int age, String name) {
			super();
			this.age = age;
			this.name = name;
}
		public String toString() {
			return this.name;
}
		public int compareTo(Object o) {
			Man man = (Man) o;
			if (this.age < man.age) {
			return -1;
}
			if (this.age > man.age) {
			return 1;
}
			return 0;
	}
}

Guess you like

Origin blog.csdn.net/Wwd631255/article/details/131060800