CodeGym /课程 /Java 语法 /使用 ArrayList 和泛型的示例

使用 ArrayList 和泛型的示例

Java 语法
第 7 级 , 课程 8
可用
使用 ArrayList 和泛型的示例 - 1

“我从真正有用的内容讲起怎么样?现在,你将看到几种可以使用 ArrayList 和泛型的方法:”

“示例 1:”

从键盘读取一列数字
public static void main(String[] args) throws IOException
{
    BufferedReader reader = new BufferedReader(new InputStreamReader(System.in) );
    ArrayList<Integer> list = new ArrayList<Integer>() ;

    while (true)
    {
        String s = reader.readLine();
        if (s.isEmpty()) break;
        list.add(Integer.parseInt(s));
    }
}

“示例 2:”

与上面相同,但偶数被添加到列表末尾,奇数被添加到列表开头。
public static void main(String[] args) throws IOException
{
    BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
    ArrayList<Integer> list = new ArrayList<Integer>();

    while (true)
    {
        String s = reader.readLine();
        if (s.isEmpty()) break;

        int x = Integer.parseInt(s);
        if (x % 2 == 0)
            list.add(x);         // 添加到末尾
        else
            list.add(0, x);      // 添加到开头           
    }
}

“示例 3:”

删除所有大于 5 的数字:
public static void main(String[] args) throws IOException
{
    BufferedReader reader = new BufferedReader(new InputStreamReader(System.in) );
    ArrayList<Integer> list = new ArrayList<Integer>();

    list.add(1);
    list.add(7);
    list.add(11);
    list.add(3);
    list.add(15);

    for (int i = 0; i < list.size(); ) // 我们将增加 i 的语句移到了循环内部 
    { 
        if (list.get(i) > 5)
            list.remove(i);  // 如果我们删除了当前元素,则不增加 i   
        else
            i++;
    }
}

“示例 4:”

将数组分为两部分 - 偶数和奇数
public static void main(String[] args) throws IOException
{
    // 数组的 static 初始化
    int[] data = {1, 5, 6, 11, 3, 15, 7, 8};  

    // 创建一个所有元素均为整数的列表 
    ArrayList<Integer> list = new ArrayList<Integer> ();

    // 使用数组填充列表
    for (int i = 0; i < data.length; i++) list.add(data[i]);  

    ArrayList<Integer> even = new ArrayList<Integer>();  // 偶数
    ArrayList<Integer> odd = new ArrayList<Integer>();    // 奇数

    for (int i = 0; i < list.size(); i++)
    {
        Integer x = list.get(i);
        if (x % 2 == 0)    // 如果 x 是偶数
            even.add(x);   // 将 x 添加到偶数集合  
        else
            odd.add(x);    // 将 x 添加到奇数集合
    }
}

“示例 5:”

合并列表
public static void main(String[] args) throws IOException
{
    ArrayList<Integer> list1 = new ArrayList<Integer>();   // 创建列表
    Collections.addAll(list1, 1, 5, 6, 11, 3, 15, 7, 8);   // 填充列表

    ArrayList<Integer> list2 = new ArrayList<Integer>();
    Collections.addAll(list2, 1, 8, 6, 21, 53, 5, 67, 18);

    ArrayList<Integer> result = new ArrayList<Integer>();

    result.addAll(list1);   // 将每个列表中的所有值添加到新列表中
    result.addAll(list2);

    for (Integer x : result)   // 快速循环遍历所有元素的方法,仅适用于集合
    {
        System.out.println(x);
    }
}

“太酷了!迭戈现在会给我一卡车类似的练习题吗?”

“是的,他会的。”

评论 (28)
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION
Anonymous #11443734 级别 22,Russia,Russian Federation
20 一月 2024
啊啊啊啊,今天刷到10级
Qin-1999 级别 22
29 十二月 2023
提示:在这类输入的任务中,如果你输入{,它会自动带出{},你应该在此时删除},因为如果你这样做,在你的输入屏变红时,你就知道你可能漏掉了某些东西或者输入错了某些东西(人们总是会眼花后破口大骂的),然后马上改正,如果看到这里,恭喜你到了这个等级,加油。
「D4C」 级别 10,China,China
26 七月 2023
“太酷了!迭戈现在会给我一卡车类似的练习题吗?” “是的,他会的。”
拔萝卜滴🐰 级别 22,chengdu,China Expert
31 一月 2023
今天是敲代码敲到吐的一天,再加把劲,到了8级我就休息
tom 级别 19,Ottawa,中国
3 一月 2024
居然还有妹纸学java
23 八月 2022
8/23
叶葳叶 级别 13,旧金山,中国
31 七月 2022
一卡车练习题沃趣
海风吹牛逼 级别 12,Hong Kong
16 二月 2022
删除所有大于 5 的数字例题中,把i++移到循环中这个想法真棒
null 级别 12
16 二月 2022
最后一个示例的Collections类为什么不要先声明创建呢?
null 级别 12
16 二月 2022
我知道了。addAll方法是Collection类的静态方法,所以可以不用创建对象直接使用
代码没写完啊~ 级别 14,Hefei,China
14 二月 2022
if (s.isEmpty()) break; 这句活中的 isEmpty 表示了什么意思,起到了什么作用
null 级别 12
16 二月 2022
String 类中 isEmpty() 方法判断字符串的长度是否为空
null 级别 12
16 二月 2022
如果字符串长度为 0,则返回 true,否则返回 false。
代码没写完啊~ 级别 14,Hefei,China
17 二月 2022
感谢大佬💪
To be brave #10900452 级别 14,Пекин,China
4 一月 2022
你在教我做事?!