标签:style array null arraylist void 一个 element 结果 main
add(index, element)
set(index, element)
使用条件
例子
public class TestList { private static List<Integer> testList; public static void main(String[] args) { initList();//初始化list System.out.println("initList="+testList.toString()); System.out.println("initList.size="+testList.size()); addList();//add list System.out.println("addList="+testList.toString()); System.out.println("addList.size="+testList.size()); System.out.println("##############################"); initList();//初始化list System.out.println("initList="+testList.toString()); System.out.println("initList.size="+testList.size()); setList();//set list System.out.println("setList="+testList.toString()); System.out.println("setList.size="+testList.size()); } /** * 初始化list */ private static void initList(){ testList = new ArrayList<Integer>(); testList.add(1); testList.add(2); testList.add(3); testList.add(4); } /** * add list */ private static void addList(){ testList.add(2, 20); } /** * set list */ private static void setList(){ testList.set(2, 20); } }
initList=[1, 2, 3, 4] initList.size=4 addList=[1, 2, 20, 3, 4] addList.size=5 ############################## initList=[1, 2, 3, 4] initList.size=4 setList=[1, 2, 20, 4] setList.size=4
public class TestListTwo { private static List<Integer> testList; public static void main(String[] args) { initList();//初始化list System.out.println("initList="+testList.toString()); Integer retValue = setList();//set list System.out.println("setList="+testList.toString()); System.out.println("setList的返回值="+retValue); } /** * 初始化list */ private static void initList(){ testList = new ArrayList<Integer>(); testList.add(1); testList.add(2); testList.add(3); testList.add(4); } /** * set list */ private static Integer setList(){ Integer retValue = testList.set(2, 20); return retValue; } }
initList=[1, 2, 3, 4] setList=[1, 2, 20, 4] setList的返回值=3
Java——List:list.add(index, element)和list.set(index, element)的区别
标签:style array null arraylist void 一个 element 结果 main
原文地址:https://www.cnblogs.com/jixue/p/10136347.html