如下所示:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
import java.util.arraylist;
//java中申请不定长度 数组
public class test01 {
public static void main(string[] args) { // todo auto-generated method stub arraylist list= new arraylist(); list.add( "123" ); list.add( "5" ); list.add( "5" );
//遍历输出list数组 for ( int i= 0 ;i<list.size();i++){ //get(index)方法,返回此列表中指定位置上的元素。 system.out.print(list.get(i).tostring()+ " " ); } system.out.println( "\n" + "遍历输出使用set方法替换后的数组list" ); /*set(index,element)方法,用指定的元素替代此列表中指定位置上的元素。 * index - 要替代的元素的索引 element - 存储在指定位置上的元素 * */ list.set( 2 , "nihao" );
//遍历输出使用set方法替换后的数组list for ( int i= 0 ;i<list.size();i++){ //get(index)方法,返回此列表中指定位置上的元素。 system.out.print(list.get(i)+ " " );
//list.size()取得list数组的长度 system.out.println( "\n" + "list数组的长度" +list.size()); } }
} |
输出结果:
1 2 3 4 |
123 5 5 遍历输出使用set方法替换后的数组list 123 5 nihao list数组的长度 3 |
以上这篇java中申请不定长度数组arraylist的方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。
原文链接:https://blog.csdn.net/qingyisuo/article/details/79240350
查看更多关于java中申请不定长度数组ArrayList的方法的详细内容...