In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)05/31 Report--
This article mainly introduces Java how to achieve two-dimensional array and sparse array conversion related knowledge, the content is detailed and easy to understand, simple and fast operation, has a certain reference value, I believe that after reading this Java how to achieve two-dimensional array and sparse array conversion article will have a harvest, let's take a look.
Two-dimensional array
A two-dimensional array is essentially an array with an array as its elements, that is, an array of arrays, the type specifier array name [constant expression] [constant expression]. A two-dimensional array is also called a matrix, and a matrix with equal number of rows and rows is called a square matrix. Symmetric matrix a [I] [j] = a [j] [I], diagonal matrix: there are zero elements outside the main diagonal of n-order square matrix.
Sparse array 1. Basic introduction of sparse algorithm
When most of the elements in an array are 0, or an array of the same value, you can use a sparse array to save the array. So as to reduce the unnecessary memory overhead of the computer.
2. The processing method of sparse algorithm
(1) the first row in the array records how many rows and columns there are in the original array and how many different values there are.
(2) record the rows and values of elements with different values in a small array, thus reducing the size of the program.
The idea of converting two-dimensional arrays into sparse arrays
Traversing the original two-dimensional array to get the number of valid data sum, according to sum can create a sparse array sparseArr int [sum+1] [3]
Store the valid data of a two-dimensional array into a sparse array
The idea of converting a sparse array to an original two-dimensional array:
First read the first row of the sparse array and create the original two-dimensional array based on the data of the first row, such as chessArr2=int [11] [11] above
Then read the last few rows of the sparse array and assign them to the original two-dimensional array.
Write a two-dimensional array:
/ / 1. Create a two-dimensional array int chessArr1 [] [] = new int [11] [11]; / / 2. Assign a value chessArr1 [1] [2] = 1 to the two-dimensional array [1] [2] [3] = 2 [2] [3] = 3. Double for loops output two-dimensional array System.out.println ("original two-dimensional array:"); for (int [] row: chessArr1) {for (int data: row) {System.out.printf ("% d\ t", data);} / / wrap one line to System.out.println () after each multiple array is output;}
Transposition two-dimensional array into sparse array
/ / convert a two-dimensional array to a sparse array / / 1. First iterate through the two-dimensional array to get the number of non-zero data. If you know the number of data, you can create a sparse array (knowing the rows of the sparse array) int sum=0;for (int I = 0; I < 11; iSuppli +) {for (int j = 0; j < 11; jacks +) {if (chessArr1 [I] [j]! = 0) {sum++ } System.out.println ("the number of valid values is:" + sum); / / 2. Create the corresponding sparse array int sparseArr [] [] = new int [sum+1] [3]; / / 3. Assign sparseArr [0] [0] = 11 sparseArr [0] [1] = 11 sparseArr [0] [2] = sum;//4 to sparse array. Traverses the two-dimensional array, giving the sparse array int count=0;// to record the row of for (int I = 0; I < 11; iSum +) {for (int j = 0; j < 11; jaun +) {if (chessArr1 [I] [j]! = 0) {sparseArr [count+1] [0] = I; sparseArr [count+1] [1] = j; sparseArr [count+1] [2] = ChessArr1 [I] [j] Count++;} / / 5. Output sparse array / / the following are two traversal methods: / * for (int [] ints: sparseArr) {for (int anInt: ints) {System.out.printf ("% d\ t", anInt);} / / change an array to a new line System.out.println ();} * / System.out.println ("get sparse array is ~"); for (int I = 0; I < sparseArr.length) System.out.printf +) {System.out.printf ("% d\ t% d\ t% d\ t\ n", sparseArr [I] [0], sparseArr [I] [1], sparseArr [I] [2]);}
Restore a sparse array to a two-dimensional array
/ / restore sparse array to two-dimensional array / / 1. Create a two-dimensional array int chessArr2 [] [] = new int [sparseArr [0] [0]] [sparseArr [0] [1]]; / / 2. Assign a value to a two-dimensional array for (int I = 1; I < sparseArr.length; Arr +) {chessArr2 [sparseArr [I] [0]] [sparseArr [I] [1]] = sparseArr [2];} / / 3. Output two-dimensional array for (int [] row: chessArr2) {for (int data: row) {System.out.printf ("% d\ t", data);} System.out.println ();}
The more the tree yearns for the light above, the more its roots go down, to the soil, to the depths of darkness.
This is the end of the article on "how to convert two-dimensional arrays to sparse arrays by Java". Thank you for reading! I believe you all have a certain understanding of "how to convert two-dimensional arrays and sparse arrays by Java". If you want to learn more, you are welcome to follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.