编程实现创建一个集合类对象,进行集合元素的添加、删除、判空等系列操作,并用4种以上方法进行集合元素的遍历输出。
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;
import java.util.Map;
import java.util.Set;
public class Demo_02 {
public static <V> void main(String[] args) {
/*
1.
æ°ç»:æ°ç»çé¿åº¦åºå®ã
éå :é¿åº¦å¯åã
*
*/
/*
*
* 2. å ç§éåçä»ç»ï¼æ¥å£ï¼
*
* 2.1ãListï¼æåºãå¯éå¤ï¼
* 2.2ãSetï¼æ åºãä¸è½éå¤ï¼
* 2.3ãMapï¼é®å¼å¯¹ãé®å¯ä¸ãå¼ä¸å¯ä¸ï¼
*
*/
//å建ä¸ä¸ªlistéå ArrayList<String>æ¯è¿ä¸ªlistéåçå®ç°ç±»
List<String> list=new ArrayList<String>();
list.add("1");
list.add("2");
list.add("4");
list.add("3");
//å¤æéåæ¯å¦ä¸ºç©º
boolean empty = list.isEmpty();
System.out.println(empty);
System.out.println(list);
//移é¤éåç´¢å¼ä¸çå¼
list.remove(0);
System.out.println(list);
list.add("1");
/* éå
åç§ç常è§è¾åºæ¹å¼ï¼
1.Iteratorï¼è¿ä»£è¾åºï¼æ¯ä½¿ç¨æå¤çè¾åºæ¹å¼ã
2.ListIteratorï¼æ¯Iteratorçåæ¥å£ï¼ä¸é¨ç¨äºè¾åºListä¸çå 容ã
3.foreachè¾åºï¼JDK1.5ä¹åæä¾çæ°åè½ï¼å¯ä»¥è¾åºæ°ç»æéåã
4.for循ç¯
*/
System.out.println("foréåï¼");
for (int i = 0; i < list.size(); i++) {
System.out.println(list.get(i));
}
System.out.println("foreachéå:");
for (String s : list) {
System.out.println(s);
}
System.out.println("iteratorçå½¢å¼ï¼");
Iterator it = list.iterator();
while(it.hasNext())
{
Object a = it.next();
System.out.println(a);
}
//ListIteratoræ¯Iteratorçåæ¥å£ï¼ä¸é¨ç¨äºè¾åºListä¸çå 容ã
System.out.println("ListIterator:");
ListIterator<String> it2 = list.listIterator();
while(it2.hasNext())
{
Object a = it2.next();
System.out.println(a);
}
//å建ä¸ä¸ªMapéå
Map map=new HashMap<String,String>();
map.put("1", "a1");
map.put("2", "a2");
map.put("4", "a4");
map.put("3", "a3");
System.out.println(map);
//å建ä¸ä¸ªSet
Set set=new HashSet<String>();
set.add("1");
set.add("2");
set.add("4");
set.add("3");
System.out.println(set);
}
}
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;
import java.util.Map;
import java.util.Set;
public class Demo_02 {
public static <V> void main(String[] args) {
/*
1.
æ°ç»:æ°ç»çé¿åº¦åºå®ã
éå :é¿åº¦å¯åã
*
*/
/*
*
* 2. å ç§éåçä»ç»ï¼æ¥å£ï¼
*
* 2.1ãListï¼æåºãå¯éå¤ï¼
* 2.2ãSetï¼æ åºãä¸è½éå¤ï¼
* 2.3ãMapï¼é®å¼å¯¹ãé®å¯ä¸ãå¼ä¸å¯ä¸ï¼
*
*/
//å建ä¸ä¸ªlistéå ArrayList<String>æ¯è¿ä¸ªlistéåçå®ç°ç±»
List<String> list=new ArrayList<String>();
list.add("1");
list.add("2");
list.add("4");
list.add("3");
//å¤æéåæ¯å¦ä¸ºç©º
boolean empty = list.isEmpty();
System.out.println(empty);
System.out.println(list);
//移é¤éåç´¢å¼ä¸çå¼
list.remove(0);
System.out.println(list);
list.add("1");
/* éå
åç§ç常è§è¾åºæ¹å¼ï¼
1.Iteratorï¼è¿ä»£è¾åºï¼æ¯ä½¿ç¨æå¤çè¾åºæ¹å¼ã
2.ListIteratorï¼æ¯Iteratorçåæ¥å£ï¼ä¸é¨ç¨äºè¾åºListä¸çå 容ã
3.foreachè¾åºï¼JDK1.5ä¹åæä¾çæ°åè½ï¼å¯ä»¥è¾åºæ°ç»æéåã
4.for循ç¯
*/
System.out.println("foréåï¼");
for (int i = 0; i < list.size(); i++) {
System.out.println(list.get(i));
}
System.out.println("foreachéå:");
for (String s : list) {
System.out.println(s);
}
System.out.println("iteratorçå½¢å¼ï¼");
Iterator it = list.iterator();
while(it.hasNext())
{
Object a = it.next();
System.out.println(a);
}
//ListIteratoræ¯Iteratorçåæ¥å£ï¼ä¸é¨ç¨äºè¾åºListä¸çå 容ã
System.out.println("ListIterator:");
ListIterator<String> it2 = list.listIterator();
while(it2.hasNext())
{
Object a = it2.next();
System.out.println(a);
}
//å建ä¸ä¸ªMapéå
Map map=new HashMap<String,String>();
map.put("1", "a1");
map.put("2", "a2");
map.put("4", "a4");
map.put("3", "a3");
System.out.println(map);
//å建ä¸ä¸ªSet
Set set=new HashSet<String>();
set.add("1");
set.add("2");
set.add("4");
set.add("3");
System.out.println(set);
}
}
温馨提示:答案为网友推荐,仅供参考