The sixth time Tutorial -- array, the sixth time tutorial

Source: Internet
Author: User

The sixth time Tutorial -- array, the sixth time tutorial

The sixth time Tutorial -- Array

This Tutorial explains how to perform Array Operations in Java, including array declaration creation and value assignment operations. The purpose of this article is to use arrays more skillfully, the following is my understanding and summary of the Code:

Hands-on brain 1: PassArray. java

 1 // PassArray.java 2 // Passing arrays and individual array elements to methods 3  4 public class PassArray { 5      6     public static void main(String[] args) { 7         int a[] = { 1, 2, 3, 4, 5 }; 8         String output = "The values of the original array are:\n"; 9 10         for (int i = 0; i < a.length; i++)11             output += "   " + a[i];12 13         output += "\n\nEffects of passing array " + "element call-by-value:\n"14                 + "a[3] before modifyElement: " + a[3];15 16         modifyElement(a[3]);17 18         output += "\na[3] after modifyElement: " + a[3];19 20         output += "\n Effects of passing entire array by reference";21 22         modifyArray(a); // array a passed call-by-reference23 24         output += "\n\nThe values of the modified array are:\n";25 26         for (int i = 0; i < a.length; i++)27             output += "   " + a[i];28         29         System.out.println(output);30     }31 32     public static void modifyArray(int b[]) {33         for (int j = 0; j < b.length; j++)34             b[j] *= 2;35     }36 37     public static void modifyElement(int e) {38         e *= 2;39     }40 41 }
PassArray. java

 

Observe and analyze the output results of the program:

The following conclusions can be drawn:

  • The greatest key to transferring array-type method parameters by reference and by value is:
    • When the former method is used, if the code in the method changes the value of the array element, the original array element is actually modified directly.
    • If the latter is used, the method body only modifies a copy of the original array element.

Start brain 2: QiPan. java

1 import java. io. *; 2 3 public class QiPan 4 {5 // defines a two-dimensional array to act as the chessboard 6 private String [] [] board; 7 // define the size of the Board 8 private static int BOARD_SIZE = 15; 9 public void initBoard () 10 {11 // initialize the checker array 12 board = new String [BOARD_SIZE] [BOARD_SIZE]; 13 // assign each element to "empty ", used to draw a checkboard 14 for (int I = 0; I <BOARD_SIZE; I ++) 15 {16 for (int j = 0; j <BOARD_SIZE; j ++) on the console) 17 {18 board [I] [j] = "yellow"; 19} 20} 21} 22 // method of outputting the board on the console 23 pu Blic void printBoard () 24 {25 // print each array element 26 for (int I = 0; I <BOARD_SIZE; I ++) 27 {28 for (int j = 0; j <BOARD_SIZE; j ++) 29 {30 // print array elements without wrapping 31 System. out. print (board [I] [j]); 32} 33 // output a linefeed 34 System after each row of array elements is printed. out. print ("\ n"); 35} 36} 37 public static void main (String [] args) throws Exception38 {39 QiPan gb = new QiPan (); 40 gb. initBoard (); 41 gb. printBoard (); 42 // This is the method for obtaining keyboard input 43 BufferedReader B R = new BufferedReader (new InputStreamReader (System. in); 44 String inputStr = null; 45 System. out. println ("Enter the coordinates of the game in the format of x and y:"); 46 // br. readLine (): when you enter a line of content on the keyboard and press enter, the entered content will be read by br. 47 while (inputStr = br. readLine ())! = Null) 48 {49 // use commas (,) as the separator to separate String 50 String [] posStrArr = inputStr. split (","); 51 // convert two strings into the coordinate of the user to play the game. The coordinate is 52 int xPos = Integer. parseInt (posStrArr [0]); 53 int yPos = Integer. parseInt (posStrArr [1]); 54 // assign the corresponding array element to "● ". 55 gb. board [xPos-1] [yPos-1] = "●"; 56/* 57 the computer randomly generates two integers as the coordinates of playing chess on the computer and assigns them to the board array. 58 also involves 59. 1. The coordinate validity can only be a number and cannot exceed 60 in the range of the board. 2. If the point of the game is to be played, you cannot play the game again. 61 3. after playing chess, you need to scan who won 62 */63 gb. printBoard (); 64 System. out. println ("Enter the coordinates of your game in the format of x and y:"); 65} 66} 67}
QiPan. java

 

Program running output result:

 

How is the chessboard represented?

The entire board is composed of "+", with 15 rows and 15 columns in total. Therefore, a 15X15 two-dimensional array can be used for representation. The position of the user playing the game is represented by the row and column positions, in this way, the array element corresponding to the user's playing position will change from "+" to "·". The structure of the Board category is as follows:

1. Private static variable BOARD_SIZE, initial value: 15;

2. Private variable Two-Dimensional String Array board [] [];

3. A total of InitBoard () methods are used to initialize the board;

4. The PrintBoard () method is used to print the board;

Start brain 3: IntToChinese. java

Problem description:

Compile a program to convert an integer to a Chinese character reading string. For example, "1123" is converted to "one thousand one hundred and twenty-three ".

Design Philosophy:

If the maximum number of digits of an integer is no more than five, the problem is not very complicated. The method for implementing this function can be defined as follows:

Source code:

1 import java. util. counter; 2 public class IntToChinese {3 4 public static void main (String [] args) {5 // TODO Auto-generated method stub 6 blocks in = new counter (System. in); 7 System. out. print ("enter an integer less than 99999:"); 8 int number = in. nextInt (); 9 System. out. println ("conversion .... "); 10 System. out. println (ToChinese (number); 11} 12 13 public static String ToChinese (int num) {14 // use Chinese characters to represent 15 String chineseNumber [] = {"0", "1", "2", "3", "4 ", "five", "Six", "Seven", "eight", "Nine"}; 16 String chinesePost [] = {"", "Ten", "Hundred ", "Thousand", "Thousand"}; 17 String result = ""; // Save the result 18 String n = Integer. toString (num); // convert a number to a string 19 char c [] = n. toCharArray (); 20 for (int I = 0; I <n. length (); I ++) {21 result + = chineseNumber [c [I]-'0'] + chinesePost [n. length ()-i-1]; 22} 23 return result; 24} 25}
IntToChinese. java

 

Running result:

 

Start brain 4: BigNumber. java

Problem description:

Use arrays to add or subtract large numbers.

Design Philosophy:

An array element accesses a number. No matter the addition or subtraction operations, the operation starts from the low position, that is, the operation starts from the last element of the array. The addition is full of 10 to 1, the maximum value of each array element is 9. If the sum of a and a is greater than 9, the previous array + a/10. The new element value is a % 10, for example:

 Source code:

[Add code]

Running result:

Result]

 

Related Article

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.