Chapter 3 Arrays and Strings (for personal use)

table of Contents:

1. One-dimensional and multi-dimensional arrays

2. Access to array elements

 

Declaration definition:

dataType[] arrayRefVar;

Statement example:

double[] myList; 

Create an array example:

arrayRefVar = new dataType[arraySize];

The above syntax statement does two things:

  • 1. Create an array using dataType[arraySize].
  • 2. Assign the reference of the newly created array to the variable arrayRefVar.

The declaration of array variables and the creation of arrays can be completed with one statement, as shown below:

dataType[] arrayRefVar = new dataType[arraySize];

In addition, you can also create an array in the following way.

dataType[] arrayRefVar = {value0, value1, ..., valuek};

The elements of the array are accessed by index. The array index starts from 0, so the index value is from 0 to arrayRefVar.length-1.

 

Code example:

public class TestArray {
   public static void main(String[] args) {
      // 数组大小
      int size = 10;
      // 定义数组
      double[] myList = new double[size];
      myList[0] = 5.6;
      myList[1] = 4.5;
      myList[2] = 3.3;
      myList[3] = 13.2;
      myList[4] = 4.0;
      myList[5] = 34.33;
      myList[6] = 34.0;
      myList[7] = 45.45;
      myList[8] = 99.993;
      myList[9] = 11123;
      // 计算所有元素的总和
      double total = 0;
      for (int i = 0; i < size; i++) {
         total += myList[i];
      }
      System.out.println("总和为: " + total);
   }
}
总和为: 11367.373

 

Creation of two-dimensional array

 

Allocate space directly for each dimension, the format is as follows:

int a[][] = new int[2][3];

 Starting from the highest dimension, allocate space for each dimension separately, for example:

String s[][] = new String[2][];
s[0] = new String[2];
s[1] = new String[3];
s[0][0] = new String("Good");
s[0][1] = new String("Luck");
s[1][0] = new String("to");
s[1][1] = new String("you");
s[1][2] = new String("!");

Arrays 类

Usage of Arrays class in java

Arrangement of common methods of Arrays class in java


 


Java array and foreach loop


String equality comparison

 

package 数组与字符串;
import  java.util.*;
import  java.io.*;
public class 字符串相等的比较 {
//    == 比较的是str 的引用的是否是同一对象
//    比较字符值是否相等要用 .equals()方法
    public static void main(String[] args) {
        String str1="hello";
        String str2="hello";
        String str3=new String("hello");
        String str4=new String("hello");

        if(str1==str2) System.out.println("yes");//yes
        else System.out.println("no");

        if(str3==str4) System.out.println("yes");//no
        else System.out.println("no");

        if(str2==str3) System.out.println("yes");//no
        else System.out.println("no");
        

        if (str1.equals(str2)) System.out.println("yes");//yes
        else System.out.println("no");

        if (str3.equals(str4)) System.out.println("yes");//yes
        else System.out.println("no");

        if (str2.equals(str3)) System.out.println("yes");//yes
        else System.out.println("no");
    }
}

Corresponding video explanation at station B

Guess you like

Origin blog.csdn.net/qq_41048982/article/details/108420410