java基础练习1

这篇博客通过一个`Person`类展示了Java的基础构造方法使用,并进行了集合操作的实战练习,包括创建ArrayList,添加不同类型的元素,使用addAll方法,检查isEmpty状态,清除集合内容,以及查询元素存在性和大小。此外,还涉及了对象作为集合元素的处理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

public class Person {
    private String name;
    private int age;

    public Person() {
    }

    public Person(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public void gomethod() {
        System.out.println("欢迎来到北京!北京欢迎你!");
    }
}
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;

/**
 * PrivateRyan
 * 2021.11.7
 * 集合强化练习
 */
public class Colection84 {
    public static void main(String[] args) {
        //创建集合,并添加元素
        Collection coll=new ArrayList();
        coll.add(new Date());
        System.out.println(coll);
        coll.add("MM");
        coll.add(896);//包装类
        coll.add(new String("魅力四射"));
        System.out.println(coll);
        Collection colle=new ArrayList();
        colle.add("希望的田野");
        coll.addAll(colle);
        System.out.println(coll);
        System.out.println(colle.isEmpty());
        colle.clear();
        System.out.println(colle);
        System.out.println("***************************************");
        System.out.println(coll.size());
        System.out.println(coll.getClass());
        System.out.println(coll.contains(896));
        System.out.println("***************************************");
        Person p=new Person("小红",20);
        coll.add(p);
        System.out.println(coll);
        System.out.println(coll.contains(p));

    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值