目錄
一.UML
①集合類圖
②線下教育平臺用例圖
?二.List集合特點
①學集合框架就是了解容器的數(shù)據(jù)結構(增刪改查)
②有序的 可重復的
三.遍歷方式
①?foreach
② iterator 迭代器
③ for
四.LinkedList
①對比ArrayList是數(shù)據(jù)結構
Linkedlist: 鏈表 特點:查詢修改慢,增加刪除快
Arraylist: 數(shù)組? 特點:查詢修改快,增加刪除慢
②堆棧 隊列
五.增長因子論證
六.集合框架ArrayList中的重復元素去重及其底層原理
一.UML
①集合類圖
②線下教育平臺用例圖
?二.List集合特點
①學集合框架就是了解容器的數(shù)據(jù)結構(增刪改查)
②有序的 可重復的
public class Demo1 {
?? ?public static void main(String[] args) {
?? ??? ?//集合本身就是一個容器,容器的特點,就是對存儲的元素進行增刪改查
?? ??? ?List list = new ArrayList<>();
?? ??? ?//增加
?? ??? ?list.add("a");
?? ??? ?list.add("b");
?? ??? ?list.add("c");
?? ??? ?System.out.println("目前集合容器中的元素:"+list);
?? ??? ?//修改
?? ??? ?list.set(1, "y");
?? ??? ?System.out.println("容器元素修改后,目前容器中的元素:"+list);
?? ??? ?//刪除
?? ??? ?list.remove(0);
?? ??? ?list.remove("a");
?? ??? ?System.out.println("容器元素刪除后,目前集合容器中的元素:"+list);
?? ??? ?//查看
?? ??? ?System.out.println("獲取第一個容器元素:"+list.get(0));
?? ?
?? ?}
?
三.遍歷方式
①?foreach
② iterator 迭代器
③ for
?? ?public static void main(String[] args) {
?? ??? ?List list = new ArrayList<>();
?? ??? ?//增加
?? ??? ?list.add("a");
?? ??? ?list.add("b");
?? ??? ?list.add("c");???????①?foreach
?? ??? ?for (Object object : list) {
?? ??? ??? ?System.out.println(object);
?? ??? ?}
?? ??? ?② iterator 迭代器
?? ??? ?Iterator it = list.iterator();
?? ??? ?while(it.hasNext()) {
?? ??? ??? ?System.out.println(it.next());
?? ??? ?}
?? ??? ?③ for
?? ??? ?for (int i = 0; i < list.size(); i++) {
?? ??? ??? ?System.out.println(list.get(i));
?? ??? ?}
?? ?}
四.LinkedList
①對比ArrayList是數(shù)據(jù)結構
Linkedlist: 鏈表 特點:查詢修改慢,增加刪除快
Arraylist: 數(shù)組? 特點:查詢修改快,增加刪除慢
②堆棧 隊列
/**
?* 用linkedList完成一個堆棧容器
?* 1.考核linkedList的api方法
?* 2.考核堆棧/隊列的數(shù)據(jù)結構特點
?* @author PC
?*
?*/
public class Demo2 {
?? ?public static void main(String[] args) {
?? ??? ?LinkedList ll = new LinkedList<>();
?? ??? ?ll.add("a");
?? ??? ?ll.add("b");
?? ??? ?ll.add("c");
?? ??? ?DuiZhan dz = new DuiZhan(ll);
?? ??? ?//定義一個方法,當這個方法被調用的時候,后存進去的元素要先輸出,先存進去的元素要后輸出
?? ??? ?System.out.println(dz.pop());
?? ??? ?System.out.println(dz.pop());
?? ??? ?System.out.println(dz.pop());
?? ?}}
class DuiZhan{
?? ?private LinkedList ll;文章來源:http://www.zghlxwxcb.cn/news/detail-536691.html?? ?public DuiZhan(LinkedList ll) {
?? ??? ?super();
?? ??? ?this.ll = ll;
?? ?}
//壓棧 ?彈棧?? ?
?? ?public Object pop() {
?? ??? ?return ll.removeLast();
?? ?}
?? ?
}
?文章來源地址http://www.zghlxwxcb.cn/news/detail-536691.html
五.增長因子論證
/**
?* linkedList調優(yōu)
?* 元素存儲在集合的過程
?* ArrayList
?* 數(shù)組 長度不可變
?*?
?* 1.證明數(shù)據(jù)結構就是數(shù)組
?* 2.為什么數(shù)組長度不可變,集合List長度可變
?*?
?* 增長因子(一次性擴容多少) 0.5倍 ?擴容1.5倍 ?1+0.5
?* @author PC
?*
?*/
public class Demo3 {
?? ?public static void main(String[] args) throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException {
?? ??? ?ArrayList al = new ArrayList<>(50);
?? ??? ?for (int i = 0; i < 100; i++) {
?? ??? ??? ?al.add(i);
?? ??? ??? ?System.out.println(i+"\r");
?? ??? ??? ?getCurrentArrLength(al);
?? ??? ?}
?? ??? ?
?? ?}?? ?//獲取ArrayList al對象底層數(shù)組的長度
?? ?private static void getCurrentArrLength(ArrayList al) throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException {
?? ??? ?Field f = al.getClass().getDeclaredField("elementData");
?? ??? ?f.setAccessible(true);
?? ??? ?Object[] Object = (java.lang.Object[]) f.get(al);
?? ??? ?System.out.println("當前集合底層數(shù)組的容器長度"+Object.length);
?? ?}
?? ??? ?
六.集合框架ArrayList中的重復元素去重及其底層原理
/**
?* list底層對象去重原理?? ?跟equals
?* @author PC
?*
?*/
public class Demo4 {
?? ?public static void main2(String[] args) throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException {
?? ??? ?List list = new ArrayList<>();
?? ??? ?list.add("a");
?? ??? ?list.add("b");
?? ??? ?list.add("c");
?? ??? ?System.out.println("目前集合容器中的"+list);
?? ??? ?//去重
?? ??? ?if(!list.contains("b")) {
?? ??? ??? ?list.add("b");
?? ??? ?}
?? ??? ?System.out.println("目前集合容器中的"+list);
?? ?}?? ?public static void main(String[] args) throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException {
?? ??? ?List list = new ArrayList<>();
?? ??? ?list.add(new Student(1,"zs"));
?? ??? ?list.add(new Student(2,"ls"));
?? ??? ?list.add(new Student(3,"lx"));
?? ??? ?System.out.println("目前集合容器中的"+list);
?? ??? ?//去重
?? ??? ?if(!list.contains(new Student(3,"lx"))) {
?? ??? ??? ?list.add(new Student(3,"lx"));
?? ??? ?}
?? ??? ?System.out.println("目前集合容器中的"+list);
?? ?}
}
class Student{
?? ?private int id;
?? ?private String name;
?? ?public int getId() {
?? ??? ?return id;
?? ?}
?? ?public void setId(int id) {
?? ??? ?this.id = id;
?? ?}
?? ?public String getName() {
?? ??? ?return name;
?? ?}
?? ?public void setName(String name) {
?? ??? ?this.name = name;
?? ?}
?? ?
?? ?public Student() {
?? ??? ?// TODO Auto-generated constructor stub
?? ?}
?? ?public Student(int id, String name) {
?? ??? ?super();
?? ??? ?this.id = id;
?? ??? ?this.name = name;
?? ?}
?? ?@Override
?? ?public String toString() {
?? ??? ?return "Student [id=" + id + ", name=" + name + "]";
?? ?}
?? ?
?? ?@Override
?? ?public boolean equals(Object obj) {
?? ??? ?System.out.println("被調了。。。");
?? ??? ?if (this == obj)
?? ??? ??? ?return true;
?? ??? ?if (obj == null)
?? ??? ??? ?return false;
?? ??? ?if (getClass() != obj.getClass())
?? ??? ??? ?return false;
?? ??? ?Student other = (Student) obj;
?? ??? ?if (id != other.id)
?? ??? ??? ?return false;
?? ??? ?if (name == null) {
?? ??? ??? ?if (other.name != null)
?? ??? ??? ??? ?return false;
?? ??? ?} else if (!name.equals(other.name))
?? ??? ??? ?return false;
?? ??? ?return true;
?? ?}
到了這里,關于J2EE&集合框架&List的文章就介紹完了。如果您還想了解更多內容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關文章,希望大家以后多多支持TOY模板網(wǎng)!