リスト反復子
import java.util.ArrayList;
import java.util.List;
//ListIterator 列表迭代器
/*
1.通过list集合的listIterator();方法得到,所以说它是List集合特有的迭代器
2.用于允许程序员沿任意方向遍历列表的列表迭代器,再迭代期间修改列表,并获取列表中迭代器的当前位置
*/
//ListIterator中的常用方法
/*
1.E next();
2.boolean hasNext();
3.E previous();
4.boolean hasPrevious();
5.void add(E e);//允许迭代的时候插入列表
*/
public class ListIterator {
public static void main(String[] args) {
//创建list集合
List<String> list=new ArrayList<String>();
//增加元素
list.add("hello");
list.add("world");
list.add("java");
//创建列表迭代器
java.util.ListIterator<String> lit = list.listIterator();
while(lit.hasNext()){
String s = lit.next();
if (s.equals("hello")){
lit.add("JAVAEE");//列表迭代器中有add方法允许迭代的时候增加元素
}
System.out.println(s);
}
System.out.println(list);
}
}
配列やコレクションをループするために強化されました
import java.util.ArrayList;
import java.util.Collection;
//增强for循环
/*
本质其实是一个迭代器Iterator,不允许在遍历的时候修改对象
格式
for(变量类型 变量名:数组或者集合)
{
此处使用变量
}
*/
public class test {
public static void main(String[] args) {
int[] a={1,2,3};
for (int aa:a){
System.out.println(aa);
}
String[] b={"a","b","c"};
for (String bb:b){
System.out.println(bb);
}
Collection<String> cc=new ArrayList<String>();
cc.add("A");
cc.add("B");
cc.add("C");
for (String c:cc){
System.out.println(c);
}
}
}
サブクラスのセットの特性を一覧表示します
import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedList;
//list常用子类
/*
1.ArrayList
数据结构是一个数组特点1.查询快2.增删慢
2.LinkedList
数据结构是一个链表1.查询慢2.增删快
*/
public class test {
public static void main(String[] args) {
//使用两个子类完成字符串存储并遍历
//创建数组集合对象
ArrayList<String> array=new ArrayList<String>();
array.add("3");
array.add("2");
array.add("1");
//用三种方式遍历数组1.迭代器2.for循环3.增强for
//迭代器
Iterator<String> it = array.iterator();
while (it.hasNext()){
String s = it.next();
System.out.println(s);
}
System.out.println("==========");
//for循环遍历
for (int i=0;i<array.size();i++){
String s2 = array.get(i);
System.out.println(s2);
}
System.out.println("==========");
//增强for循环
for (String s3:array){
System.out.println(s3);
}
System.out.println("==========");
//创建链表集合对象
LinkedList<String> linkedList=new LinkedList<>();
linkedList.add("A");
linkedList.add("B");
linkedList.add("C");
//用三种方式遍历数组1.迭代器2.for循环3.增强for
//迭代器
Iterator<String> it2 = linkedList.iterator();
while (it2.hasNext()){
String s = it2.next();
System.out.println(s);
}
System.out.println("==========");
//for循环遍历
for (int i=0;i<linkedList.size();i++){
String s=linkedList.get(i);
System.out.println(s);
}
System.out.println("==========");
//增强for循环
for (String s:linkedList){
System.out.println(s);
}
}
}
LinkedList
コレクションのユニークな方法
import java.util.LinkedList;
public class LinkedListTest {
public static void main(String[] args) {
LinkedList<String> linkedList=new LinkedList<String>();
linkedList.add("hello");
linkedList.add("world");
linkedList.add("java");
System.out.println(linkedList);
}
}
セットコレクションの概要と特長
//Set集合概述和特点
/*
Set集合特点
-不包含重复元素的集合
-没有带索引的方法,所以不能使用普通for循环遍历
HashSet:对集合的迭代顺序不作任何保证
*/
public class test {
public static void main(String[] args) {
//Set集合练习 存储字符串并遍历
//创建集合对象
Set<String> set=new HashSet<String>();
//增加集合元素
set.add("hello");
set.add("world");
set.add("java");
//遍历Set集合
for (String s:set){
System.out.println(s);
}
}
}
ハッシュ値(ハッシュ)
//哈希值:是JDK根据对象的地址或者字符串或者数字算出来的int类型的数值
/*Object类中有一个方法可以获得对象的哈希值
public int hashCode();返回对象的哈希码值
*/
public class HashTest {
public static void main(String[] args) {
//创建学生对象
Student s1 = new Student("yang",18);
Student s2 = new Student("sheng",18);
//多次调用hashCode方法得到的哈希值是一样的
System.out.println(s1.hashCode());//460141958
System.out.println(s1.hashCode());//460141958
//默认情况下,不同对象的哈希值是不相同的
//通过重写方法,可以实现不同对象的哈希值是相同的
System.out.println(s2.hashCode());//1163157884
System.out.println("重地".hashCode());//1179395
System.out.println("通话".hashCode());//1179395
}
}
HashSetのコレクション機能
HashSet集合特点
-底层数据结构是哈希表
-对集合的迭代顺序不做任何保证,不保证存储和去除的元素顺序一致
-没有带索引的方法,不能用普通for遍历
—由于是Set集合,所以不包含重复元素的集合
*/
public class HashSetTest {
public static void main(String[] args) {
//练习:存储字符串并遍历
//创建一个集合
HashSet<String> c=new HashSet<String>();
//集合中增加元素
c.add("hello");
c.add("world");
c.add("java");
c.add("java");//不包含重复元素
//遍历HashSet
for (String s:c){
System.out.println(s);
}
}
}
ソースコード解析の一意性を確保するためのHashSetのコレクション要素
//创建一个集合
HashSet<String> c=new HashSet<String>();
//集合中增加元素
c.add("hello");
c.add("world");
c.add("java");
c.add("java");//不包含重复元素
public boolean add(E e) {
return map.put(e, PRESENT)==null;
}
public V put(K key, V value) {
return putVal(hash(key), key, value, false, true);//hash(e)方法返回了e的哈希值
}
static final int hash(Object key) {
int h;
return (key == null) ? 0 : (h = key.hashCode()) ^ (h >>> 16);
}
final V putVal(int hash, K key, V value, boolean onlyIfAbsent,boolean evict) {
Node<K,V>[] tab; Node<K,V> p; int n, i;
//如果哈希表未初始化,就对哈希表进行初始化
if ((tab = table) == null || (n = tab.length) == 0)
n = (tab = resize()).length;
//根据对象的哈希值计算存储位置,如果位置没有元素就存储对象
if ((p = tab[i = (n - 1) & hash]) == null)
tab[i] = newNode(hash, key, value, null);
else {
Node<K,V> e; K k;
/*
存入的元素和以前的元素比较哈希值
如果哈希值不同,会继续向下执行,把元素添加到集合
如果哈希值相同,会调用对象的equals()方法比较
如果返回false,会继续向下执行,把元素添加到集合
如果返回true,说明元素重复,不存储
*/
if (p.hash == hash &&
((k = p.key) == key || (key != null && key.equals(k))))
e = p;
else if (p instanceof TreeNode)
e = ((TreeNode<K,V>)p).putTreeVal(this, tab, hash, key, value);
else {
for (int binCount = 0; ; ++binCount) {
if ((e = p.next) == null) {
p.next = newNode(hash, key, value, null);
if (binCount >= TREEIFY_THRESHOLD - 1) // -1 for 1st
treeifyBin(tab, hash);
break;
}
if (e.hash == hash &&
((k = e.key) == key || (key != null && key.equals(k))))
break;
p = e;
}
}
if (e != null) { // existing mapping for key
V oldValue = e.value;
if (!onlyIfAbsent || oldValue == null)
e.value = value;
afterNodeAccess(e);
return oldValue;
}
}
++modCount;
if (++size > threshold)
resize();
afterNodeInsertion(evict);
return null;
}