1. Value passing and reference passing from arrays 2. Relearn arrays (1) primitive type array (2) reference type array 3. The answer to the question is

1. Value passing and reference passing from arrays

Let’s do a code problem output problem

public class DemoCollection14 {
    public static void main(String[] args) {

        String [] strs = {"zs"."ls"."wu"};

        for (String str : strs) {
            strs[0] = null;
            System.out.println(str);
        }

        
        for(String str : strs) { System.out.println(str); }}}/ / output:
// zs
// ls
// wu
//
// null
// ls
// wuTo understand this problem, read the explanation belowCopy the code

2. Relearn arrays

(Teacher Liao Xuefeng’s explanation is quoted here)

(1) Array of primitive types

Arrays are reference types and their sizes are immutable

public class Main {
    public static void main(String[] args) {
        // 5 students' scores:
        int[] ns;
        ns = new int[] { 68.79.91.85.62 };
        System.out.println(ns.length); / / 5
        ns = new int[] { 1.2.3 };
        System.out.println(ns.length); / / 3}}Copy the code

Does the array size change? It looks like it’s changed, but it hasn’t.

For array ns, ns = new int[] {68, 79, 91, 85, 62}; , it points to an array of five elements:

Ns │ ▼ ┌ ─ ─ ─ ┬ ─ ─ ─ ┬ ─ ─ ─ ┬ ─ ─ ─ ┬ ─ ─ ─ ┬ ─ ─ ─ ┬ ─ ─ ─ ┐ │ │ │ │ │ │ 85 91 79 68 62 │ │ └ ─ ─ ─ ┴ ─ ─ ─ ┴ ─ ─ ─ ┴ ─ ─ ─ ┴ ─ ─ ─ ┴ ─ ─ ─ ┴ ─ ─ ─ ┘Copy the code

Ns = new int[] {1, 2, 3}; When, it points to a new three-element array:

Ns ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ┐ │ ▼ ┌ ─ ─ ─ ┬ ─ ─ ─ ┬ ─ ─ ─ ┬ ─ ─ ─ ┬ ─ ─ ─ ┬ ─ ─ ─ ┬ ─ ─ ─ ┬ ─ ─ ─ ┬ ─ ─ ─ ┬ ─ ─ ─ ┬ ─ ─ ─ ┐ │ │ │ │ │ │ 85 91 79 68 62 │ │ 1 2 3 │ │ │ │ └ ─ ─ ─ ┴ ─ ─ ─ ┴ ─ ─ ─ ┴ ─ ─ ─ ┴ ─ ─ ─ ┴ ─ ─ ─ ┴ ─ ─ ─ ┴ ─ ─ ─ ┴ ─ ─ ─ ┴ ─ ─ ─ ┴ ─ ─ ─ ┘Copy the code

However, the original array of five elements is unchanged; they are simply not referenced by the ns variable.

(2) String array

If an array element is not a primitive type, but a reference type, how would it be different to modify an array element?

Strings are references, so we define an array of strings:

String[] names = {
    "ABC"."XYZ"."zoo"
};
Copy the code

For the array variable names of type String[], it actually contains three elements, but each element refers to some String object:

┌ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ┐ names │ ┌ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ┼ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ┐ │ │ │ │ │ ▼ │ │ ▼ ▼ ┌ ─ ─ ─ ┬ ─ ─ ─ ┬ ─ ┴ ─ ┬ ─ ┴ ─ ┬ ─ ─ ─ ┬ ─ ─ ─ ─ ─ ─ ─ ┬ ─ ─ ─ ┬ ─ ─ ─ ─ ─ ─ ─ ┬ ─ ─ ─ ┬ ─ ─ ─ ─ ─ ─ ─ ┬ ─ ─ ─ ┐ │ │ ░ ░ ░ │ ░ ░ ░ │ ░ ░ ░ │ │ "ABC" │ │ "XYZ" │ │ "zoo" │ │ └ ─ ─ ─ ┴ ─ ┬ ─ ┴ ─ ─ ─ ┴ ─ ─ ─ ┴ ─ ─ ─ ┴ ─ ─ ─ ─ ─ ─ ─ ┴ ─ ─ ─ ┴ ─ ─ ─ ─ ─ ─ ─ ┴ ─ ─ ─ ┴ ─ ─ ─ ─ ─ ─ ─ ┴ ─ ─ ─ ┘ │ bring └ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ┘Copy the code

Assign names[1], for example, names[1] = “cat”; , the effect is as follows:

┌ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ┐ names │ ┌ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ┐ │ │ │ │ │ │ ▼ │ │ ▼ ▼ ┌ ─ ─ ─ ┬ ─ ─ ─ ┬ ─ ┴ ─ ┬ ─ ┴ ─ ┬ ─ ─ ─ ┬ ─ ─ ─ ─ ─ ─ ─ ┬ ─ ─ ─ ┬ ─ ─ ─ ─ ─ ─ ─ ┬ ─ ─ ─ ┬ ─ ─ ─ ─ ─ ─ ─ ┬ ─ ─ ─ ┬ ─ ─ ─ ─ ─ ─ ─ ┬ ─ ─ ─ ┐ │ │ ░ ░ ░ │ ░ ░ ░ │ ░ ░ ░ │ │ "ABC" │ │ "XYZ" │ │ "zoo" │ │ "Cat" │ │ └ ─ ─ ─ ┴ ─ ┬ ─ ┴ ─ ─ ─ ┴ ─ ─ ─ ┴ ─ ─ ─ ┴ ─ ─ ─ ─ ─ ─ ─ ┴ ─ ─ ─ ┴ ─ ─ ─ ─ ─ ─ ─ ┴ ─ ─ ─ ┴ ─ ─ ─ ─ ─ ─ ─ ┴ ─ ─ ─ ┴ ─ ─ ─ ─ ─ ─ ─ ┴ ─ ─ ─ ┘ │ bring └ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ┘Copy the code

Notice that the string “XYZ” to which names[1] refers has not been changed, only that the reference to names[1] has been changed from pointing to “XYZ” to pointing to “cat”. The result is that the string “XYZ” is no longer accessible through names[1]. With a better understanding of pointing, try explaining the following code:

public class Main {
    public static void main(String[] args) {
        String[] names = {"ABC"."XYZ"."zoo"};
        String s = names[1];
        names[1] = "cat";
        System.out.println(s); Is s "XYZ" or "cat"?}}/ / output "XYZ"

// Explain why:
// After the names string array is created, s points to names[1].
// However, instead of changing the original data XYZ to cat, name[1] redirects to the spatial address where cat is stored.
// so s is still pointing to the original position, so XYZ is output
Copy the code

3. What is the answer to the question

To return to the original question:

public class DemoCollection14 {
    public static void main(String[] args) {

        String [] strs = {"zs"."ls"."wu"};

        for (String str : strs) {
            strs[0] = null;
            System.out.println(str);
        }

        
        for(String str : strs) { System.out.println(str); }}}/ / output:
// zs
// ls
// wu
//
// null
// ls
// wuForeach is just an iterator. Iterator, not in the traditional senseforLoop over the array data. Instead, we define String STR, in order STR = STRS [I], and print STR. So it's the same thing as xyz.Copy the code