This is the fifth day of my participation in the August More text Challenge. For details, see:August is more challenging

The article directories

  • Create an array
  • Access to an array
  • The length of the array
  • Array traversal
  • Array utility class java.util.Arrays
  • Introduction to Two-dimensional Arrays

Create an array

The first way to create

int[] a = new int[5];
Copy the code

Create an array of length 5 (0); create an array of length 5 (0); create an array of length 5 (0); create an array of length 5 (0); create an array of length 5 (0)

The second way to create

int[] a = {1.2.3.4.5};
Copy the code

The third way to create

Assign a new array to the array variable a

a = new int[] {6.7.8};
Copy the code

Access to an array

Gets the value of index 0

a[0]
Copy the code

The length of the array

a.length
Copy the code

2. The maximum subscript value is a.length-1. 3. The length of the array can be 0

Array traversal

for (int i = 0; i < a.length; i++) {
	a[i];
}
Copy the code

Exercise: Randomly generate a bicolor ball

Two-color ball game rules there are 6 groups of red ball, each group from 1 to 33 to draw one, six do not repeat each other. And then the blue ball takes a number from 1 to 16, and this whole thing makes up the bicolor ball

xml

    <Button
        android:id="@+id/button"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Randomly generate bicolor balls." />

    <TextView
        android:id="@+id/red"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:textColor="#FF0000"
        android:textSize="18sp" />

    <TextView
        android:id="@+id/blue"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:textColor="#0000FF"
        android:textSize="18sp" />
Copy the code
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    Button button;
    TextView redText;
    TextView blueText;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        button = findViewById(R.id.button);
        button.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        int[] r = new int[33];
        int[] b = new int[16];
        for (int i = 0; i < 33; i++) {
            r[i] = i + 1;
        }
        for (int i = 0; i < 16; i++) {
            b[i] = i + 1;
        }
        // Convert the array to String output
        Log.d("Bicolor"."Red:" + Arrays.toString(r));
        Log.d("Bicolor"."Blue" + Arrays.toString(b));

        // Red ball result array and tag array
        int[] red = new int[6];
        boolean[] flag = new boolean[33];

        for (int i = 0; i < 6; i++) {
            int j;
            do {
                // select a random subscript from the r array and assign it to
                j = new Random().nextInt(33);
            } while (flag[j]);// If yes, re-select
            red[i] = r[j];
            flag[j] = true;
        }

        // The result array
        int blue = b[new Random().nextInt(16)];
        // Display the result in two TextViews
        redText = findViewById(R.id.red);
        blueText = findViewById(R.id.blue);

        redText.setText("Red ball:" + Arrays.toString(red));
        blueText.setText("Basketball:"+ blue); }}Copy the code

Run the program and the results are as follows;

Practice: Guess letters

public class Main {
    public static void main(String[] args) {
        // Randomly generate 5 unrepeated uppercase letters
        char[] s = random();

        System.out.println("5 unrepeated uppercase letters have been generated");

        while (true) {
            System.out.println("Guess");
            // Get the string entered by the user
            String input = new Scanner(System.in).nextLine();
            // To compare with the array above, convert the string to an array of characters
            char[] inputResult = input.toCharArray();

            String result = compare(inputResult, s);
            System.out.println(result);
            // If the result is "5A0B", the end
            if (result.equals("5A0B")) {
                break; }}}static char[] random() {
        // Create a char array of length 26
        char[] c = new char[26];
        // Insert A-Z in order
        for (int i = 0; i < c.length; i++) {
            c[i] = (char) ('A' + i);
        }
        // Visit the first five locations. The purpose of this step is to transpose the first five letters and the next five letters to produce non-repeating letters
        for (int i = 0; i < 5; i++) {
            int j = i + new Random().nextInt(26 - i);
            char temp = c[i];
            c[i] = c[j];
            c[j] = temp;
        }
        System.out.println(Arrays.toString(c));
        // Cut the first five letters to form a new array
        char[] result = Arrays.copyOf(c, 5);
        System.out.println("The first five letters after the interception:" + Arrays.toString(result));
        return result;
    }


    static String compare(char[] input, char[] result) {
        int a = 0;
        int b = 0;
        for (int i = 0; i < 5; i++) {
            for (int j = 0; j < 5; j++) {
                if (input[i] == result[j]) {
                    if (i == j) {
                        // Letters are equal and positions are equal
                        a++;
                    } else {
                        // The letters are equal, but the positions are different
                        b++;
                    }
                    // There are 5 different letters, so if you find the same one, break
                    break; }}}return a + "A" + b + "B"; }}Copy the code

Running result:

Array utility class java.util.Arrays

Arrays.tostring Concatenated values in Arrays [value 1, value 2]

Arrays.copyOf Copies an array to a new array of specified length, creating a new array

System.arraycopy(original array, original array start position, target array, target array start position, number of copies); Copy the array

The Arrays. Sort (array); Quick sort algorithm for array sort optimization

Arrays.binarySearch(Object[], Object key); Binary search, so the array must be sorted or the function of sort() is to find the specified element in the sorted array by dichotomy and return the index of that element

1, return the subscript of the element in the array if the element is present. 2, return -(insertion point + 1) if the element is not present. The insertion point here refers specifically to the subscript of that element in the array, if it exists in the array

Fill (array, 1); Fills the array with the specified value

All of these are very useful, so you can test them for yourself

Introduction to Two-dimensional Arrays

A two-dimensional array is an array that stores a one-dimensional array (memory address/reference)

Create an array

int[][] a = new int[3] [2];
Copy the code

2. The length of the outer array is 3, and each of the inner arrays is 2. 3. The memory address of an array is stored in the outer array

int[][] a = new int[3] [];Copy the code

A [0] = new int[5]; a[0] = new int[5]; A [1] = new int[]{5,3,7,4} a[2] = new int[]{5,3,7,4}

You can also assign values directly

int[][] a = {
	{2.1.5.6},
	{4.5},
	{2.4}};Copy the code

access

a[0] [1]
a[2] [0]
Copy the code

traverse

for (int i = 0; i < a.length; i++) {
	for (int j = 0; j < a[i].length; j++) { System.out.println(a[i][j]); }}Copy the code