关于ArrayList的个人总结(第一次发表还请大家多多指点)

 

package cn.practice;

//新闻标题类: ID 新闻标题  创建者
public class NewsTitle {
	private int ID;
	private String title;
	private String author;

	public NewsTitle() {

	}

	public NewsTitle(int ID, String title, String author) {
		this.ID = ID;
		this.title = title;
		this.author = author;
	}

	public int getID() {
		return ID;
	}

	public void setID(int iD) {
		ID = iD;
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public String getAuthor() {
		return author;
	}

	public void setAuthor(String author) {
		this.author = author;
	}
//	public String toString(){
//		return title;
//	}
}

 

package cn.practice;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

//对新闻标题的存储及操作
public class NewsMgr {
	public static void main(String[] args) {
		// 创建新闻标题对象
		NewsTitle title1 = new NewsTitle(1, "北京热门景点故宫1", "author");
		NewsTitle title2 = new NewsTitle(1, "北京热门景点长城2", "author");
		NewsTitle title3 = new NewsTitle(1, "北京热门景点颐和园3", "author");
		NewsTitle title4 = new NewsTitle(1, "北京热门景点北海4", "author");
		NewsTitle title5 = new NewsTitle(1, "北京热门景点天安门5", "author");
		// 创建集合对象,并将新闻标题加入到集合对象
		// 导入ArrayList包
		// ArrayList list =new ArrayList();
		// List list = new ArrayList();
		// 父类引用指向子类对象,但是这种写法只能调用父子类共有的方法,无法调取子类独有的方法;
		ArrayList list = new ArrayList<>();
		list.add(title1);// 等同于list[0] = title1;
		list.add(title2);
		list.add(title3);
		list.add(title4);
		// 指定在某个插入元素
		list.add(1, title5);

		// 获取新闻标题的总数
		// ArrayList的size()方法等同于数组的length属性的作用
		System.out.println("新闻标题一共有" + list.size() + "条");
		// 逐条打印新闻标题的名称
		System.out.println("*******************************************************");
		// 方法一:遍历ArrayList元素的位置(下标)
		for (int i = 0; i < list.size(); i++) {
			NewsTitle title = (NewsTitle) list.get(i);// 等同于list[i],请注意,返回值为object
														// 强制类型转换为NewsTitle类
			System.out.println(title.getTitle());

		}
		System.out.println("*******************************************************");
		// 方法二:增强型for
		for (Object obj : list) {
			NewsTitle title = (NewsTitle) obj;
			System.out.println(title.getTitle() + title.getAuthor() + title.getID());
		}
		System.out.println("*******************************************************");
		// 创建迭代器进行循环
		Iterator it = list.iterator();
		System.out.println("元素是");
		while (it.hasNext()) {// 判断下一位元素是否存在
			// Object obj = it.next();
			// System.out.println(obj);
			NewsTitle news = (NewsTitle) it.next();// Object类不能直接调用子类对象及方法,所以要进行强转!
			System.out.println(news.getTitle());
		}
		// 列表中是否含有某个元素
		System.out.println(list.contains(title5));
		// 删除指定位置的列表元素
		// list.remove(0);
		// 删除指定元素
		list.remove(title1);
		// 判断列表中是否存在指定元素
		System.out.println(list.contains(title1));

	}
}

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值