Understanding arrays -- sparse arrays

Posted by slysop on Fri, 11 Feb 2022 05:02:31 +0100

Understanding arrays -- sparse arrays

Let's talk about some recent understandings and hope to enlighten you:

1. Programs exist for the realization of functions, so in the process of learning, we should first understand the principle of function realization, and then remember the key code process; (that is, at the beginning of learning, you can learn flexibly only by learning to die - remember)

2. How can * * be more efficient when learning the basic concepts of a language** You should first find the relevant concepts that you must understand before you get started, and then search for relevant simple questions with the leedcode keyword. It doesn't matter if you don't have ideas. Just look at the answer first, understand each sentence of code, understand the code logic, and then remember, so as to go through a cycle. It is said that it will be painful for at least three months==

3. What is the significance of algorithms and data structures for me to write java code?

Clarify the relationship between data structure and algorithm from the perspective of analyzing problems. Generally, each problem is solved through the following two steps:
Analyze the problem, extract valuable data from the problem and store it;
Process the stored data and finally get the answer to the question;

The data structure is responsible for solving the first problem, that is, the storage of data. Through the previous study, we know that for different logical and physical structures of data, the optimal data storage structure can be selected to store data.

The remaining second problem belongs to the responsibility of the algorithm. Algorithm is understood from the surface meaning, that is, the method to solve the problem. We know that the evaluation of an algorithm depends on which algorithm has the highest efficiency on the premise of solving the same problem, and the efficiency here refers to the ability to process and analyze data.

Therefore, we come to the conclusion that data structures are used to solve the problem of data storage, while algorithms are used to process and analyze data. They are completely different disciplines.
-------—

The above is from the excerpt. I found the answer on this brother's post. Thank you for sharing

Original link: https://blog.csdn.net/alan711/article/details/86739788

Print out the original array

Reduce the size of the following array to (sparse list)

Idea of printing original array:

1. Create a new array and assign a value to the coordinates of the array with significant numbers

2. Use foreach to traverse the loop and output all elements of the original array

	public static void main(String[] args) {
		int[][] array1=new int[11][11];//Remember to give the array size when creating a new array
		array1[1][2]=1;
		array1[2][3]=2;
		System.out.println("Please output the original array:");
		//foreach traversal loop statement:
		for(int[] ints:array1) {//For (type variable name: Set) {statement block;}
//			System.out.print(ints+"\t");
	//Ints is not a circular variable. It saves the elements in the set. The for each statement takes out the elements in the set one by one and saves them in ints
    /*The first traversal of a two-dimensional array is the address, and the second traversal is the specific value. The essence of a two-dimensional array is to nest an array in an array, and an array is a collection.
	array1 It is a two-dimensional array. It is traversed for the first time: the address of each line is stored in ints, and the set is also stored in the address book.
	Then traverse the values in the ints set. After traversing, each element is stored in anInt. Finally, the specific values are stored in anInt and output*/
	//There is an address before each line[ I@7637f22 	 0 	 0 	 0 	 0 	 0 	 0 	 0 	 0 	 0 	 0 	 0	
			for(int anInt :ints) {
				System.out.print(anInt+"\t");
	         }
			System.out.println();
		}
Conversion to sparse array storage idea:

1. Obtain the number of coordinates with valid values (element value in coordinates! = 0) through circular traversal

2. Create a new container array array2 to hold the sparse array, and also give some initial known coordinate values in array2

3. Traverse the two-dimensional array and store the non-zero value in the sparse array

4. Output sparse array

//Get the number of valid values
		int sum=0;
		 for(int i=0;i<11;i++) {
			 for(int j=0;j<11;j++) {
				 if(array1[i][j]!=0) {// Write the value in if(array1[][]!=0)
					 sum++;//Through the above two figures, we find that the number of the last row of the sparse list is exactly equal to the number of effective numbers
				 }
			 }
		 }
		 System.out.println("Number of valid values:"+sum);
		 
		//Create a sparse array 
		 int[][] array2=new int[sum+1][3];//int[sum+1][3] defines the size of array2 array
		 array2[0][0]=11;
		 array2[0][1]=11;
		 array2[0][2]=sum;
		 //Traverse the two-dimensional array and store the non-zero value in the sparse array
		 int count=0;
		 for(int i=0;i<array1.length;i++) {
			 for(int j=0;j<array1[i].length;j++ ) {//array1[i]. Length I is the length of the line
				 if(array1[i][j]!=0) {
					 count++;//First, you have to list the style of sparse array on the draft paper. 0 column: row 1 column: column; Column 2: value
					 array2[count][0]=i;
					 array2[count][1]=j;
					 array2[count][2]=array1[i][j];
				 }
			 }
		 }
		
		 //Output sparse array
		 System.out.println("==============");
		 System.out.println("Sparse array:");
		 for(int i=0;i<array2.length;i++) {
			 System.out.println( array2[i][0]+"\t"+
			 					array2[i][1]+"\t"+
			 					array2[i][2] +"\t");
		 }
	  
Ideas for restoring sparse arrays:

1. Read the sparse array and use the new container array3 to load the read array value. (note that the size of the new array3 array is defined)

2. Convert the coordinate elements in array2 into the original array array1 and load them into the array3 container.

3. Traverse array array3 output elements.

 System.out.println("==============");
		 System.out.println("Restore sparse array:");
		 
		 //1. Read sparse array
		int[][] array3=new int[array2[0][0]][array2[0][1]];
		 /*int[array2[0][0]][array2[0][1]] array2 Is a sparse array,
		 The first element in the first row is: the total number of rows in array1 group;
		 The second element in the first row is: the total number of columns in array1 group;
		 */
		 //Restore its value to the element in it
		 for(int i=1;i<array2.length;i++) {//Line 0 does not need to be traversed
		 array3[array2[i][0]][array2[i][1]]=array2[i][2];
		 }
		 //Print
		 System.out.println("==============");
		 System.out.println("Output original array:");

		 for(int[] ints:array3){  
				for(int anInt :ints) {
					System.out.print(anInt+"\t");
		         }
				System.out.println();
			}

		 for(int[] ints:array3)  {
				for(int anInt :ints) {
					System.out.print(anInt+"\t");
		         }
				System.out.println();
			}

Topics: Java