JAVA集合框架和泛型笔记

package cn.bdqn.list;
//新闻标题类
public class NewsTitle{
	//ID	新闻标题	  作者
	private int id;
	private String title;
	private String uathor;
	public NewsTitle() {
	}
	public NewsTitle(int id, String title, String uathor) {
		this.id = id;
		this.title = title;
		this.uathor = uathor;
	}
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getTitle() {
		return title;
	}
	public void setTitle(String title) {
		this.title = title;
	}
	public String getUathor() {
		return uathor;
	}
	public void setUathor(String uathor) {
		this.uathor = uathor;
	}
	
}
package cn.bdqn.list;

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

//新闻管理ArrayList简单应用
public class ArrayListDemo{
	public static void main(String[] args) {
		//集合存储多条新闻标题
		NewsTitle title1=new NewsTitle(1,"东莞终于晴了","admin");
		NewsTitle title2=new NewsTitle(2,"武汉终于晴了","admin");
		NewsTitle title3=new NewsTitle(3,"杭州终于晴了","admin");
		NewsTitle title4=new NewsTitle(4,"深圳终于晴了","admin");
		NewsTitle title5=new NewsTitle(5,"上海终于晴了","admin");
		ArrayList list=new ArrayList();
		list.add(title1);
		list.add(title2);
		list.add(title3);
		list.add(title4);
		list.add(0,title5);
		System.out.println("新闻标题的总数:"+list.size());
		//遍历list,取出每条新闻标题的题目 1.1普通for遍历
		for(int i=0;i

ArrayList 常用方法;
遍历查看集合中的元素效率较高,插入删除元素效率较低
方法名 说明
boolean add(Object 0) 在列表的末尾顺序添加元素,起始索引位置从零开始
void add(int index,Object 0) 在指定的索引位置添加元素。索引位置必须介于0和列表元素个数之间,LinkedLists使用时效率更高
int size() 返回列表中的元素个数
Object get(int index) 返回指定索引位置处的元素。取出的元素是Object类型,使用前需要进行强制类型转换
boolean contains(Object 0) 判断列表中是否存在指定元素
boolean remove(Object 0) 从列表中删除元素
Object remove(int index)

从列表中删除指定位置元素,起始索引位置从0开始

LinkedList相比ArrayList的独有常用方法First和Last
LinkedList插入和删除元素效率更高
removeFirst() 移除第一个元素
removeLast() 移除最后一个元素
addFirst() 在第一个元素前插入元素
addLast() 在最末一个元素后插入元素
getFirst() 获得第一个位置的元素,需要强制类型转换
getLast() 获得最后一个位置元素,需要强制类型转换


Map使用
key键——value值
Map con = new HashMap();//父类引用指向子类对象
put(); 往集合中添加键值对,例:con.put("CN","中国");
size(); 获取键值对对数
get(); 通过键,获取对应的值,例:String cnSer = (String)con.get("CN")
containskey() 判断Map中是否包含某个键
containsvalue() 判断Map中是否包含某个值
keySet() 键的集合
values() 值的集合
直接打印对象名 键值对的集合,输出:[CN=中国]
clear() 清空,if(isEmpty()){System.out.println("Map数据已清空");} isEmpty判断Map是否为空

遍历方法获取元素 适用
普通for ArrayList,     LinkedList
增强for ArrayList,     LinkedList,    Set
Iterator ArrayList,     LinkedList,     Set,    

以上笔记可能整理不全,希望各位大神在评论区补充。
也希望各位会学习的大神给小弟分享一些学习的方法,和做笔记的方法。

你可能感兴趣的:(java)