<> Use of two-dimensional arrays
Two dimensional array arr[ 2 ] [ 3 ] The element of is a one-dimensional array , If you visit page 2 Number of arrays 2 Elements are arr[2][2]
<> Binary array syntax
grammar : type [ ] [ ] Array name = new type [ size ] [ size ]
grammar : type Array name [ ] [ ] ; Array name = new type [ size ] [ size ]
grammar : type Array name [ ] [ ] = { {1,2,3} , {4,5,6,7,8} , {2,3,1} }
/* Print 00000 00100 01240 14656 */ public class text { public static void main(
String[] args) { int [][]arr = {{0,0,0,0,0},{0,0,1,0,0},{0,1,2,4,0},{1,4,6,5,6}}
; for(int i = 0;i < arr.length; i++) { //arr.length It refers to the number of two-dimensional array elements for(int k = 0 ;
k< arr[i].length ; k++) {//arr[i].length It refers to the number of two-dimensional array elements System.out.print(arr[i][k]+
"\t"); } System.out.println(); } } }
Print m array 1 2 2 3 3 3 public class text { public static void main(String[] args) {
int m[][] = { {1},{2,2},{3,3,3} }; for(int i = 0; i<m.length; i++ ) {
// Traversing a two-dimensional array 3 Arrays for(int k = 0; k <m[i].length ; k++) { System.out.print(m[i][k]+
"\t");// Traverse each element of each array } System.out.println(); } } }
Two dimensional array printing multiplication formula table
public class text { public static void main(String[] args) { int m[][] = new
int [9][] ; // Uncertain number of columns , Can not write for(int i = 0; i<m.length; i++ ) { // Traversing a two-dimensional array 3 Arrays m[i]
= new int [ i+1]; // Loop to open each one-dimensional array in the two-dimensional array , and i+1 Affecting the following m[i].length Length of for(int k = 0;
k<m[i].length ; k++) { m[i][k]= i + 1; System.out.print(m[i][k]+"*"+(k+1)+"="+m[
i][k]*(k+1)+"\t");// Traverse each element of each array } System.out.println(); } } }
Print ten lines Yanghui triangle
public class text { public static void main(String[] args) { int m[][] = new
int[10][];// Define a two-dimensional array of ten rows for(int i = 0; i < m.length;i++ ) { m[i]= new int [i +1]
; // open up m[0],m[1]........m[9] Different spaces for(int k =0; k < m[i].length; k++) {
// Traverse all elements contained in each unary array in a binary array if( (k == 0) || (k == m[i].length -1 )) { m[i][k]=1; }
else { m[i][k]= m[i-1][k-1]+m[i-1][k]; } } } for(int i = 0; i < m.length;i++ ) {
for( int k =0; k < m[i].length; k++) { System.out.print(m[i][k]+"\t"); } System.
out.println(); } } }
An array {10,12,45,90} Is ascending , After a number is required to be inserted , Or ascending
public class text { public static void main(String[] args) { int arr[] = {10,12
,45,90}; int insertNum = 23; int index = -1;// index Is where you want to insert it //** location **
// Set a variable index Used to receive Subscripts i, Sequential traversal Number of inserts inserNum and Elements of the original array arr[i] Compare , // if
Number of inserts insertNum Small , Then use index Get current element subscript i; Otherwise, the subscript index Assign maximum subscript arr[arr.length] for(int i= 0;
i<arr.length;i++) { if( insertNum <= arr[i]) { index = i; break; } } if(index ==
-1) { index = arr.length; } System.out.println(" location index The location of is "+index); //** Capacity expansion **
// Create a new array , And assign the original array to the new array in turn int arr_new[] = new int [arr.length+1]; for (int i =
0,j=0; i < arr_new.length; i++) { if( i != index ) { arr_new[i] = arr[j]; j++; }
else { arr_new[i] = insertNum; } } // Give Way arr point arr_new, Recycle original arr Space for arr = arr_new;
// Traversal output System.out.println("----- Sorting after adding -----"); for(int i=0;i<arr.length;i++) {
System.out.print(arr[i]+"\t"); } } }
Lottery realization
import java.util.Random; import java.util.Scanner; public class text {
// Create a function , For line feed public static void nt () { System.out.println(""); System.out.
println(""); System.out.println(""); } public static void main(String[] args) {
Scanner scanner = new Scanner(System.in); Random rand = new Random();
// For random each red and blue ball int redArr[] = new int[6]; int blueArr[] = new int[1]; // Used to receive each red and blue ball of the user
int insertRed[] = new int[6]; int insertBlue[] = new int[1]; // Random red blue number for(int i
= 0; i<redArr.length; i++) { redArr[i] = rand.nextInt(33 - 1 + 1) + 1; } blueArr
[0] = rand.nextInt(16 - 1 + 1) + 1; // Matching red and blue winning numbers int index1 = 0; // Used to define the number of times in the red ball int
index2= 0;// Used to define the number of times in the blue ball int num =1; for(int i = 0; i<redArr.length; i++) { System
.out.println(" Please enter page "+num+" Red ball number [1-33]"); insertRed[i] = scanner.nextInt(); num++;
for(int j =0 ; j<redArr.length; j++) { if(insertRed[i] == redArr[j] ) { index1++
; } } } System.out.println(" Please enter 1 Blue ball numbers [1-16]"); insertBlue[0] = scanner.nextInt(
); if(insertBlue[0] == blueArr[0] ) { index2++; } nt (); if( index1== 6 &&
index2== 1 ) { System.out.println(" My God? , The arrival of the European Emperor , Won the first prize 1000 Ten thousand yuan "); } else if( index1== 6
) { System.out.println(" A burst of luck , Won the second prize 500 Ten thousand yuan "); } else if( index1== 5 && index2 == 1
) { System.out.println(" Not bad , Won the third prize 3000 element "); } else if( index1== 5 || (index1==4 &&
index2== 1) ) { System.out.println(" hey , Won the fourth prize 200 element "); } else if( index1== 4 || (
index1==3 && index2 == 1) ) { System.out.println(" Oh, Ho , Won the fifth prize 10 element "); } else if(
index2== 1 ) { System.out.println(" Not bad not bad , Won the sixth prize 5 element "); } else { System.out.println(
" Thank you for your patronage , Thank you for your contribution to people's livelihood "); } nt(); System.out.println(" Check the winning number of this welfare lottery (y/n)"); char x =
scanner.next().charAt(0); if( x == 'y') { // Announce the final award number . System.out.println(
"**** The winning number of this lottery is ****"); nt(); System.out.println(" The red balls are "); for(int i = 0; i<redArr
.length; i++) { System.out.print(redArr[i]+"\t"); } nt(); System.out.println(
" The blue balls are "); for(int i = 0; i<blueArr.length; i++) { System.out.print(blueArr[i]+
"\t"); } nt(); } } }
<> Considerations for 2D arrays
Definition method
one-dimensional : int arr [ ] = new int [x ]; two-dimensional : int arr [ ][ ] = new int [ x][ y];
A two bit array is composed of multiple one bit arrays , The length of each one bit array can be the same , It can also be different
arr [ ][ ] = { {1,23},{43,65,76},{22,56…87.33,24} }
Technology
Daily Recommendation