Java List<String> The solution to the method of passing parameters and failing to return a value

The solution to passing parameters in the Java List method and not being able to return a value encountered a magical problem today. A List was passed in the Java method, and a value was assigned to it in the method. Confirm that there is a value before returning to the method. Outside the method not available.


I encountered a magical problem today. A List is passed in the Java method, and a value is assigned to it in the method. It is confirmed that there is a value before returning to the method, and it cannot be obtained outside the method.

1. Questions

The Java method passes the parameter list, and the value is successfully modified in the method, but the value is not obtained after returning.

2. Causes and solutions

Reason: The newList here is new, it is a new piece of memory, but the address of stringList is pointed to newList in the method, so the value we use in the method is the value of newList, and once the scope in the method ends , newList is destroyed, stringList has nothing to do with the value of newList, so it seems that the address is not passed.

Solution: 1. List.addAll in the method; 2. The return value is list; 3. List.add() in the method;

3. Source code

package com.utils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 对list的各种赋值方式的测试
 * 找到整体新的list赋值传递到方法外仍然有效的的方法
 * addAll,add,return List
 */
public class TestList {
    
    
    public static void main(String[] args) {
    
    
        TestList test = new TestList();

        List<String> stringList0 = new ArrayList<>();
        test.streamSet(stringList0);
        System.out.println("长度是" + stringList0.size());//0

        List<String> stringList2 = new ArrayList<>();
        test.equalList(stringList2);
        System.out.println("长度是" + stringList2.size());//0

        List<String> stringList3 = new ArrayList<>();
        test.arrayAsList(stringList3);
        System.out.println("长度是" + stringList3.size());//0

        // 解决办法·1
        List<String> stringList1 = new ArrayList<>();
        test.addList(stringList1);
        System.out.println("长度是" + stringList1.size());//1

        // 解决办法·2
        List<String> stringList4 = new ArrayList<>();
        test.newArrayAsList(stringList4);
        System.out.println("长度是" + stringList4.size());//2

        // 解决办法·3
        List<String> stringList5 = new ArrayList<>();
        stringList5 = test.returnList();
        System.out.println("长度是" + stringList5.size());//2
    }

    private void streamSet(List<String> stringList0) {
    
    
        String[] strings = "1,2,3,4,5".split(",");
        stringList0 = Arrays.stream(strings).collect(Collectors.toList());
    }

    /**
     * add方法往入参的list传值
     * 地址传递,有效
     *
     * @param stringList list
     */
    public void addList(List<String> stringList) {
    
    
        stringList.add("add");
    }

    /**
     * 方法内的new的list赋值给入参的list
     * 方法外无效
     *
     * @param stringList list
     */
    public void equalList(List<String> stringList) {
    
    
        List<String> newList = new ArrayList<>();
        newList.add("new");
        stringList = newList;
    }

    /**
     * 数组通过Arrays.asList方法转化为list赋值给入参的list
     * 方法外无效 Arrays.asList的实现机制是new
     *
     * @param stringList list
     */
    public void arrayAsList(List<String> stringList) {
    
    
        String[] arrayStr = {
    
    "arrayStr1", "arrayStr2"};
        List<String> newList = Arrays.asList(arrayStr);
        stringList = newList;
    }

    /**
     * 使用list的addAll方法将new的list传递给入参list
     * addAll是list提供的用于整体赋值list的方法,相当于将每项取出来多次遍历去add
     *
     * @param stringList list
     */
    public void newArrayAsList(List<String> stringList) {
    
    
        String[] arrayStr = {
    
    "arrayStr1", "arrayStr2"};
        List<String> newList = Arrays.asList(arrayStr);
        stringList.addAll(newList);
        //List不支持clone方法,但是addAll很好用
    }

    /**
     * 方法内的list做返回值返回到方法外
     */
    public List<String> returnList() {
    
    
        String[] arrayStr = {
    
    "arrayStr1", "arrayStr2"};
        List<String> newList = Arrays.asList(arrayStr);
        return newList;
    }
}

reference

Guess you like

Origin blog.csdn.net/qq_40985985/article/details/130226076