Java ArrayList

1、构造方法 //5月14日

Arraylist()

构造一个长度可变的数组 集合对象

使用时

Arraylist list=new Arraylist ();

后面 <> 以及里面的内容可以省略,但编译器会给出提示,最好还是带上。

例如` public static void main(String[] args) {

    ArrayList<String> list1=new ArrayList<String>();  
    list.add("assd");  
    list.add("ssss");  
    list.add("fffff");  
    System.out.println(list);  

    ArrayList<String> list=new ArrayList<String>(list);  
    System.out.println(list);  `

2. ArrayList 中常用的方法

size(): 数组中实际元素个数

get(int index): 返回数组指定位置的元素

remove( e ): 移除数组中的某个元素

3.for循环,foreach循环输出public class ForEachInstanceForList {

public class chenyang {  
    public static void main(String[] args) {  
        ArrayList<String> list = new ArrayList<>(); 
            list.add("a");  
        for (int i = 0; i < 5; i++) {  
        chenyang f = new chenyang();
            System.out.println(f.gei(i)); 
        }  
        for (String c : list) {  //任意名字+数组名     list 为数组名
            System.out.println(c);  
        }  
    }  
}  

4.next()与nextLine()

nextLine()方法返回的是Enter键之前的所有字符,它是可以得到带空格的字符串的。

next()会自动消去有效字符前的空格,只返回输入的字符,不能得到带空格的字符串。
import java.util.Scanner;  


public class chenyang {  

    public static void main(String[] args) {  

        String s2;    
        int s1;  
        Scanner sc=new Scanner(System.in);    
        System.out.print("请输入第一个整数:");    
        s1=sc.nextInt();   
        sc.nextLine();  
        System.out.println("输入的整数是:"+s1);  

        System.out.print("请输入第二个字符串:");   
        s2=sc.nextLine();   
        System.out.println("输入的字符串是:"+s2);  

        }  
}  
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值