An array of Java articles

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

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

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 9, 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:

Inttochinese.java

Operation Result:

Expansion Program:

Further, can you change the amount represented in the numbers to "Chinese character expression?" For example, the "¥123.52" converted to "one Bai San Yuan Wu angle of the three points."

Implementation code:

String Tobigletter (double money)

Results:

Design ideas:

Simply separate the decimal point from the back and then split it separately, using the following methods:

String.substring ();

String.IndexOf ();

Specific ideas can be deepened by the comments in the code.

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:

Bignumber.java

Operation Result:

An array of Java articles

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.