SparseArray (sparse array) introduction and application

Bloggers statement:
reprint, please link at the beginning of this article and additional author information, and marked as reserved. This article from the original blogger Whiskers Meow, like him support and advice.
This article first appeared in this blogger: Whiskers Meow | blog home page: https://blog.csdn.net/smile_running

Sparse array concepts and features introduced

Sparse translation is sparse, lack of meaning, SparseArray is sparse array. It is relatively rare scenario data, performance data is generally within a few hundred HashMap relatively better, about 0-50% to enhance performance. SparseArray with a key mapping Integer object.

SparseArray not required key and value for auto-boxing (autoboxing), the original package type object type, such as the package type int type Integer.

10W insertion of data storage using DDMS view, HashMap storage space around 14M, and SparseArray only about 8M, nearly 40% less memory. SparseArray efficiency than the positive-sequence reverse insertion is inserted is almost 10 times faster.

If the insert is in the order of 3,2, but will be rearranged in the positive sequence within SparseArray, SparseArray This is because when used in the search data is binary search (dichotomy must be ordered), it each time new data is inserted when SparseArray need to reorder, reverse is the worst case.

Backgammon game

The concept introduced above, look like, there are online, so I will not say more.
Reference to the following, free to look:
https://www.cnblogs.com/Diyo/p/11305850.html
https://www.jianshu.com/p/081b78dfe9f6

Next, I talk about my understanding. I directly from one case to analyze this case in fact we have played, it is a backgammon game , backgammon play very simple, which has such a scenario. For example, I need to sunspots and white stones on the board to save them, because the player may want to archive .
Archive it means to be stored locally, how to save up pieces on the board, surely we might think of one of the most direct way is to use a two-dimensional array .

For example, this board is a 6 * 5 of
Here Insert Picture Description
our two-dimensional array to store, then it should be like this

	// 6*5 的二维数组
	private static int[][] array = new int[][]{
		{0,3,0,0,0},
		{0,0,0,7,0},
		{0,0,0,0,0},
		{0,0,0,0,0},
		{0,0,0,0,0},
		{0,0,0,0,0}
	};
Problem leads

You can see, the two-dimensional array, in fact, not much problem. But if you want to store, then the value of most of the elements in the array is zero, almost no sense. True RMS, only 3 and 7 , on behalf of the pieces on the map coordinates .
If such a two-dimensional array will be saved to disk or memory, it occupies memory space is 6 * 5 * int bytes.

SparseArray (sparse array) introduced the principle of

SparseArray principle: the majority is saved in the array is 0 or a value of the same element, a first two-dimensional array of storage row rows and columns, and the number of valid elements , labeled with different ranks of the other element values and its location.
Sparse array has a feature that its columns are fixed and only three, the number of rows = rms + 1, because the 0th row for storing two-dimensional array information.
So, it will be the board's two-dimensional array, the following looks into
Here Insert Picture Description
the case, from an array of just 6 * 5, all of a sudden turned into a 3x3 array of memory occupied by clearly more low.

Two-dimensional array -> sparse arrays

In fact, if we know what the principle is sparse array, it is how to convert a two-dimensional array, then in terms of the code, it should not be a problem. Here is what I wrote a two-dimensional array to a sparse array of code:

	/**
	 * 二维数组 -> 稀疏数组
	 * @param arrs
	 */
	public static void arrayToSparseArray(int[][] arrs) {
		int row = arrs.length;
		int col = arrs[0].length;
		int valCount = 0;

		// 用于存储临时元素
		int size = row * col;
		int tempI[] = new int[size];
		int tempJ[] = new int[size];
		int tempArray[] = new int[size];

		// 获取二维数组中的有效值
		for (int i = 0; i < row; i++) {
			for (int j = 0; j < col; j++) {
				if (arrs[i][j] > 0) {
					tempI[valCount] = i;
					tempJ[valCount] = j;
					tempArray[valCount] = arrs[i][j];

					valCount++;
					System.out.println("i = " + i + ", j = " + j
							+ ", realVal = " + arrs[i][j]);
				}
			}
		}

		int spRow = valCount + 1;// 行数 = 有效值 + 1
		int[][] spArray = new int[spRow][3];
		// 设置稀疏数组第 0 行的值
		spArray[0][0] = row;
		spArray[0][1] = col;
		spArray[0][2] = valCount;

		// 为稀疏数组赋值
		for (int i = 0; i < valCount; i++) {
			spArray[i + 1][0] = tempI[i];
			spArray[i + 1][1] = tempJ[i];
			spArray[i + 1][2] = tempArray[i];
		}

		System.out.println("========================");
		printArray(spArray);
		System.out.println("========================");
		sparseArrayToArray(spArray);
	}

Sparse array -> two-dimensional array

Sparse array to a two-dimensional array also Similarly, convert ideas turn on the line

	/**
	 * 稀疏数组 -> 二维数组
	 * @param arrs
	 */
	public static void sparseArrayToArray(int[][] arrs) {
		// 获取稀疏数组中第一行保存的二维数组的行、列和有效值个数
		int row = arrs[0][0];
		int col = arrs[0][1];
		int valCount = arrs[0][2];

		// 用于存储临时元素
		int tempI[] = new int[valCount];
		int tempJ[] = new int[valCount];
		int tempArray[] = new int[valCount];

		// 获取稀疏数组中的 i,j,val,做临时保存
		for (int i = 0; i < valCount; i++) {
			tempI[i] = arrs[i + 1][0];
			tempJ[i] = arrs[i + 1][1];
			tempArray[i] = arrs[i + 1][2];
		}

		// 初始化一个默认为 0 的二维数组
		int[][] array = new int[row][col];
		for (int i = 0; i < row; i++) {
			for (int j = 0; j < col; j++) {
				array[i][j] = 0;
			}
		}

		// 为二维数组设置有效值
		for (int i = 0; i < valCount; i++) {
			array[tempI[i]][tempJ[i]] = tempArray[i];
		}


		printArray(array);
	}

Print elements in the array:

	public static void printArray(int[][] arrs) {
		int row = arrs.length;
		int col = arrs[0].length;
		for (int i = 0; i < row; i++) {
			for (int j = 0; j < col; j++) {
				System.out.println("i = " + i + ", j = " + j + ", val = "
						+ arrs[i][j]);
			}
		}
	}

Codes do not do too much to explain, and relatively simple to understand.

Published 101 original articles · won praise 766 · views 870 000 +

Guess you like

Origin blog.csdn.net/smile_Running/article/details/103915900