Dark Horse Programmer----Java Learning Notes array, two-dimensional array, with related questions

Source: Internet
Author: User

------<a href= "http://www.itheima.com" target= "blank" >java training, Android training, iOS training,. NET training </a>, look forward to communicating with you! -------

One: Array (master)

(1) Array: A container that stores multiple elements of the same data type.
(2) Features: Each element has a number, starting from 0, the maximum number is the length-1.
Number of professional names: Index
(3) Define the format
A: Data type [] array name;
B: Data type array name [];

Recommendation is a way, B method forget it. such as defining an int array. Int[] A.
But to be able to read
(4) Initialization of arrays
A: Dynamic Initialization
Give only the length, the system gives the default value

Example: int[] arr = new INT[3];
B: Static initialization
Given the value, the system determines the length

Example: int[] arr = new int[]{1,2,3};
Simplified version: int[] arr = {£ º};
(5) memory allocation in Java
A: Stack stores local variables
B: Heap Storage for all new
C: Method Area (object-oriented part explained in detail)
D: Local Method Area (System-dependent)
E: Register (CPU use)

Attention:
A: A variable that is defined by a local variable in a method or a method declaration.
B: The difference between stack memory and heap memory
Stack: When the data is used, it disappears.
Heap: Every new one comes with an address.
Each variable has a default value
Byte,short,int,long 0
float,double 0.0
Char ' \u0000 '
Boolean false
Reference type NULL

When the data in the heap memory is used, it is not immediately recycled and is reclaimed when the garbage collector is idle.

Plot of stack memory and heap memory


(6) Array Memory graph
A: An array
B: Two arrays
C: Three arrays (two stack variables point to the same heap memory)
(7) Common operations for arrays
A: Traverse
Mode 1:
public static void PrintArray (int[] arr) {
for (int x=0; x<arr.length; x + +) {
System.out.println (Arr[x]);
}
}

Mode 2:
public static void PrintArray (int[] arr) {
System.out.print ("[");
for (int x=0; x<arr.length; x + +) {
if (x = = arr.length-1) {
System.out.println (arr[x]+ "]");
}else {
System.out.println (arr[x]+ ",");
}
}
}
B: Best Value
Maximum value:
public static int Getmax (int[] arr) {
int max = arr[0];

for (int x=1; x<arr.length; x + +) {
if (Arr[x] > max) {
max = arr[x];
}
}

return Max;
}

Minimum value:
public static int getmin (int[] arr) {
int min = arr[0];

for (int x=1; x<arr.length; x + +) {
if (Arr[x] < min) {
min = arr[x];
}
}

return min;
}
C: Reverse Order
Mode 1:
public static void reverse (int[] arr) {
for (int x=0; x<arr.length/2; x + +) {
int temp = arr[x];
ARR[X] = Arr[arr.length-1-x];
Arr[arr.length-1-x] = temp;
}
}

Mode 2:
public static void reverse (int[] arr) {
for (int start=0,end=arr.length-1; start<=end; start++,end--) {
int temp = Arr[start];
Arr[start] = Arr[end];
Arr[end] = temp;
}
}
D: Check Table
public static String getString (string[] Strarray,int index) {
return Strarray[index];
}
E: Basic Search
Mode 1:
public static int GetIndex (int[] arr,int value) {
for (int x=0; x<arr.length; x + +) {
if (arr[x] = = value) {
return x;
}
}

return-1;
}

Mode 2:
public static int GetIndex (int[] arr,int value) {
int index =-1;

for (int x=0; x<arr.length; x + +) {
if (arr[x] = = value) {
index = x;
Break
}
}

return index;
}

Two-dimensional Array (understanding)
(1) The element is an array of one-dimensional arrays.
(2) Format:
A: Data type [] Array name = new data type [dynamic initialization of the m][n];//array.
B: Data type [] Array name = new data type [m][];//does not give the number of one-dimensional array elements, which can be given dynamically.
C: Data type [] Array name = new data type [][]{{...},{...},{...}};
D: Data type [] Array name = {{...},{...},{}};

Example: int[] x,y[]; where X is a one-dimensional array and y is a two-dimensional array.  The code is actually equivalent to int[] x; Int[] y[];


(3) Related questions (master):

A: With two-dimensional array output: Yang Hui triangle

The code is as follows

/* Need: Print Yang Hui triangle (number of lines can be keyboard input) 11 11 2 11 3 3 11 4 6 4 1 1 5 10 10 5 1 Analysis: Look at the pattern of this image a: the first and last columns of any row are 1 B: Starting from the third row, each data is the previous column of its previous row and the column And. Step: A: First define a two-dimensional array.  If the number of rows is N, we define the number of columns as n first. This n data comes from keyboard entry. B: Assign a value of 1 C to the first and last column of any row of this two-dimensional array: By law, the other elements are assigned a value starting at the third row, each of which is the sum of the previous column of its previous row and the column on its previous row. D: Traverse the two-dimensional array. */import java.util.scanner;class array2test3 {public static void main (string[] args) {//Create keyboard Entry Object Scanner sc = new Scanner (S ystem.in);//The data for this n is from keyboard entry. System.out.println ("Please enter a data:"); int n = sc.nextint ();//define a two-dimensional array int[][] arr = new int[n][n];//Assign a value of 1for to the first and last column of any row of this two-dimensional array (int x=0; x<arr.length; x + +) {Arr[x][0] = 1;//any row 1th column arr[x][x] = 1;//The last 1 columns of any row}//assign values to other elements by law//starting with the third row, each data is the sum of the previous column of its previous row and the column on its previous row. for (int x=2; x<arr.length; x + +) {//here if Y<=x is a minor problem, that is the last column of the question//So here to subtract 1//and y should also start from 1, because the first column also has a value for (int y=1; y <=x-1; y++) {//each data is the sum of the previous column of its previous row and the column of its previous row. Arr[x][y] = Arr[x-1][y-1] + arr[x-1][y];}} Iterate over this two-dimensional array. /*for (int x=0; x<arr.length; x + +) {for (int y=0; y<arr[x].length; y++) {System.out.print (arr[x][y]+ "\ t");} System.out.println ();} *///thisTime, note that the inner loop must change from the 99 multiplication table that was said for (int x=0; x<arr.length; x + +) {for (int y=0; y<=x; y++) {System.out.print (arr[x][ y]+ "\ t");} System.out.println ();}}}

B: Sum of two-dimensional arrays

The code is as follows

/* Company Annual sales sum the data of a company according to quarterly and monthly statistics are as follows: unit (million yuan) first quarter: 22,66,44 second quarter: 77,33,88 third quarter: 25,45,65 fourth quarter: 11,66,99 Analysis: A: The data of the topic is represented by a two-dimensional array int[] [] arr = {{22,66,44},{77,33,88},{25,45,65},{11,66,99}}; B: How to sum up? The sum is actually to get each element and then accumulate. C: Defines a sum variable sum, which is initialized with a value of 0. D: The elements of each two-dimensional array can be obtained by traversing. E: Accumulate elements. F: The final output sum is the result. */class array2test2 {public static void main (string[] args) {//The data of the topic is represented by a two-dimensional array int[][] arr = {{22,66,44},{77,33,88},{25,45 , 65},{11,66,99}};//defines a sum variable sum, initialized with a value of 0. int sum = 0;//The elements of each two-dimensional array can be obtained by traversing. for (int x=0; x<arr.length; x + +) {for (int y=0; y<arr[x].length; y++) {//Add elements. Sum + = Arr[x][y];}} The final output sum is the result. System.out.println ("Sales for the Year:" +sum+ "million");}}

C: Traversal of two-dimensional arrays

The code is as follows

/* Requirements: Two-dimensional array traversal outer loop control is the length of the two-dimensional array, in fact, is the number of one-dimensional arrays. The inner loop controls the length of a one-dimensional array. */class array2test {public static void main (string[] args) {//define a two-dimensional array int[][] arr = {{1,2,3},{4,5,6},{7,8,9}};//ask who represents { 1,2,3}//arr[0] is the first array//arr[0] = {1,2,3};for (int x=0; x<arr[0].length; x + +) {System.out.println (arr[0][x]);} System.out.println ("--------------"); for (int x=0; x<arr[1].length; x + +) {System.out.println (arr[1][x]);} System.out.println ("--------------"); for (int x=0; x<arr[2].length; x + +) {System.out.println (arr[2][x]);} System.out.println ("--------------");//Use loops to improve for (int x=0; x<3; x + +) {for (int y=0; y<arr[x].length; y++) { System.out.print (arr[x][y]+ "");} System.out.println ();} System.out.println ("--------------");//This time, notice that 3 is what we get out of the code above//However, it cannot be used for any array can be like this//So, we should try to improve//actually, The length of the outer loop is the length of the two-dimensional array for (int x=0; x<arr.length; x + +) {for (int y=0; y<arr[x].length; y++) {System.out.print (arr[x][ y]+ "");} System.out.println ();} System.out.println ("--------------");//method to improve//call method PrintArray2 (arr); System.out.println ("--------------");//We'll do it again. The number of columns is int[][] arr2 = {{1,2,3},{4,5},{6}};p RintArray2 (ARR2);} /* Requirements: traverse a two-dimensional array two explicit: return value type: void parameter list: int[][] arr*/public static void PrintArray2 (int[][] arr) {for (int x=0; x<arr.length; X + +) {for (int y=0; y<arr[x].length; y++) {System.out.print (arr[x][y]+ "");} System.out.println ();}}}

------<a href= "http://www.itheima.com" target= "blank" >java training, Android training, iOS training,. NET training </a>, look forward to communicating with you! -------

Dark Horse Programmer----Java Learning Notes array, two-dimensional array, with related questions

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.