Action brain, Sixth time Tutorial--array

Source: Internet
Author: User

Action brain, Sixth time Tutorial--array

This tutorial explains how to do array operations in java, including the use of array declaration creation and assignment operations, The purpose of writing this article is to use the actual application has reached a more proficient array, the following is the practice code after the sentiment and summary:

Hands on Brain 1:Passarray.java

1 //Passarray.java2 //passing arrays and individual array elements to methods3 4  public classPassarray {5     6      public Static voidmain (string[] Args) {7         inta[] = {1, 2, 3, 4, 5 };8String output = "the values of the original array are:\n";9 Ten          for(inti = 0; I < a.length; i++) oneOutput + = "" +a[i]; a  -Output + = "\n\neffects of passing array" + "element call-by-value:\n" -+ "a[3] before modifyelement:" + a[3]; the  -Modifyelement (a[3]); -  -Output + = "\na[3" after modifyelement: "+ a[3]; +  -Output + = "\ N Effects of passing entire array by reference"; +  aModifyarray (a);//array a passed call-by-reference at  -Output + = "\n\nthe values of the modified array are:\n"; -  -          for(inti = 0; I < a.length; i++) -Output + = "" +a[i]; -          in System.out.println (output); -     } to  +      public Static voidModifyarray (intB[]) { -          for(intj = 0; J < b.length; J + +) theb[j] *= 2; *     } $ Panax Notoginseng      public Static voidModifyelement (intE) { -E *= 2; the     } +  a}
Passarray.java

Observe and analyze the output of the program:

The following conclusions can be drawn:

    • The biggest key to passing the array type method parameters by reference and by value Is:
      • When using the former, if there is code in the method that changes the value of the array element, the original array element is actually modified Directly.
      • Using the latter does not have the problem, and the method body modifies only one copy of the original array Element.

Hands on Brain 2:Qipan.java

1 ImportJava.io.*;2 3  public classQipan4 {5     //define a two-dimensional array to act as a chessboard6     Privatestring[][] board;7     //define the size of the board8     Private Static intBoard_size = 15;9      public voidInitboard ()Ten     { one         //initializing an array of checkers aboard =Newstring[board_size][board_size]; -         //assign each element "╋" to draw a chessboard on the console -          for(inti = 0; I < board_size; i++) the         { -              for(intj = 0; J < board_size; J + +) -             { -board[i][j] = "╋"; +             } -         } +     } a     //How to output a checkerboard in the console at      public voidPrintboard () -     { -         //Print each array element -          for(inti = 0; I < board_size; i++) -         { -              for(intj = 0; J < board_size; J + +) in             { -                 //print array elements without wrapping to System.out.print (board[i][j]); +             } -             //outputs a newline character after each line of array elements is printed theSystem.out.print ("\ n"); *         } $     }Panax Notoginseng      public Static voidMain (string[] Args)throwsException -     { theQipan GB =NewQipan (); + Gb.initboard (); a Gb.printboard (); the         //This is the method for getting keyboard input +BufferedReader br =NewBufferedReader (NewInputStreamReader (system.in)); -String Inputstr =NULL; $System.out.println ("please Enter the coordinates of your chess, should be in the format x, y:"); $         //br.readline (): Whenever you enter a line on the keyboard press enter, the content you just entered will be read by Br.  -          while((inputstr = Br.readline ())! =NULL) -         { the             //separates the user-entered string into 2 strings with a comma (,) as a delimiter -string[] Posstrarr = Inputstr.split (",");Wuyi             //converts 2 strings to the coordinates of a user's chess the             intXPos = Integer.parseint (posstrarr[0]); -             intYPos = Integer.parseint (posstrarr[1]); wu             //assign the corresponding array element as "".  -gb.board[xpos-1][ypos-1] = "";  about             /* $ the computer randomly generates 2 integers, which are assigned to the board array as the coordinates of a computer's chess game.  - also involves - 1. The validity of the coordinates, can only be a number, not beyond the board range - 2. If the point of chess under, can not repeat Chess.  a 3. After each play, you need to scan who wins . +              */ the Gb.printboard (); -System.out.println ("please Enter the coordinates of your chess, should be in the format x, y:"); $         } the     } the}
Qipan.java

Program Run output Result:

How is the chessboard represented?

The whole chessboard is composed of a "+", a total of 15 rows and 15 columns, so you can use a 15x15 two-dimensional array representation, the location of the user playing chess is represented by the row and column position, so that the User's position on the playing chess array elements will be changed from "+" to "•". The Board class structure is as Follows:

1. Private static variable board_size, initial value is 15;

2. Private variable two-dimensional string array board[][];

3. Common method Initboard (), initialize the chessboard;

4. Common Method Printboard (), print the board;

Hands on Brain 3:Inttochinese.java

Problem Description:

Write a program to convert an integer to a kanji read string. For example, "1123" is converted to "1123".

Design Ideas:

If the maximum number of digits in this integer is not more than 5, then the problem is not very complicated. The way to do this is to define:

    1. Defines a string array of size 10 chinesenumber, The initial value is: "0", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine";
    2. Defines a string array of size 5 chinesepost, The initial value is: "", "ten", "hundred", "thousand", "million";
    3. For the function to get the parameters num, the number of split analysis and the number of each bit, num if it is a string type may be convenient, combined with the above two arrays to convert Chinese characters can be;

Source:

1 Importjava.util.Scanner;2  public classInttochinese {3 4      public Static voidmain (string[] Args) {5         //TODO auto-generated Method Stub6Scanner in =NewScanner (system.in);7System.out.print ("please Enter an integer within 99999:");8         intNumber =In.nextint ();9System.out.println ("in Transition ....") ");Ten System.out.println (tochinese (number)); one     } a      -      public StaticString Tochinese (intNum) { -         //the Arabic numerals are expressed in Chinese characters theString chinesenumber[] = {"0", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"}; -String chinesepost[] = {"", "ten", "hundred", "thousand", "million"}; -String result = "";//Store Results -String n = integer.tostring (num);//convert a number to a string +         Charc[] =N.tochararray (); -          for(inti = 0; I < N.length (); i++) { +Result + = chinesenumber[c[i]-' 0 ']+chinesepost[n.length ()-i-1]; a         } at         returnresult; -     } -}
Inttochinese.java

Operation Result:

Hands on Brain 4:Bignumber.java

Problem Description:

The use of arrays for large number addition and Subtraction.

Design Ideas:

An array element accesses a single digit, either addition or subtraction, starting from the low, that is, the last element of the array begins the operation, the addition is 10 in 1, the highest value for each array element is 9, the sum of the added and a exceeds 9 let the previous array +a/10, and the new element value is a%10, such as:

Source Code:

"add code"

Operation Result:

Results

Action brain, Sixth time Tutorial--array

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.