public static void main(String[] args) {
// 创建一个空的数组链表对象list,list用来存放String类型的数据
List<String> list = new ArrayList<String>();
// 增加元素到list对象中
list.add("Item1");
list.add("Item2");
list.add(2, "Item3"); // 此条语句将会把“Item3”字符串增加到list的第3个位置。
list.add("Item4");
// 显示数组链表中的内容
System.out.println("The arraylist contains the following elements: " + list);
// 检查元素的位置
int pos = list.indexOf("Item2");
System.out.println("The index of Item2 is: " + pos);
// 检查数组链表是否为空
boolean check = list.isEmpty();
System.out.println("Checking if the arraylist is empty: " + check);
// 获取链表的大小
int size = list.size();
System.out.println("The size of the list is: " + size);
// 检查数组链表中是否包含某元素
boolean element = list.contains("Item5");
System.out.println("Checking if the arraylist contains the object Item5: " + element);
// 获取指定位置上的元素
String item = list.get(0);
System.out.println("The item is the index 0 is: " + item);
// 遍历arraylist中的元素
// 第1种方法: 循环使用元素的索引和链表的大小
System.out.println("Retrieving items with loop using index and size list");
for (int i = 0; i < list.size(); i++) {
System.out.println("Index: " + i + " - Item: " + list.get(i));
}
// 第2种方法:使用foreach循环
System.out.println("Retrieving items using foreach loop");
for (String str : list) {
System.out.println("Item is: " + str);
}
// 第三种方法:使用迭代器
// hasNext(): 返回true表示链表链表中还有元素
// next(): 返回下一个元素
System.out.println("Retrieving items using iterator");
for (Iterator<String> it = list.iterator(); it.hasNext();) {
System.out.println("Item is: " + it.next());
}
// 替换元素
list.set(1, "NewItem");
System.out.println("The arraylist after the replacement is: " + list);
// 移除元素
// 移除第0个位置上的元素
list.remove(0);
// 移除第一次找到的 "Item3"元素
list.remove("Item3");
System.out.println("The final contents of the arraylist are: " + list);
// 转换 ArrayList 为 Array
String[] simpleArray = list.toArray(new String[list.size()]);
System.out
.println("The array created after the conversion of our arraylist is: " + Arrays.toString(simpleArray));
}
}
Set集合
public static void main(String[] args) {
Set<String> set = new HashSet<String>(); // 创建一个空的 Set 集合
String name1 = new String("李雨");
String name2 = new String("赵益");
String name3 = new String("戴文浩");
set.add(name1);
set.add(name2);
set.add(name3);
set.add(name1);
// set.remove(name3);
System.out.println("学生列表:");
Iterator<String> it = set.iterator();
while (it.hasNext()) {
System.out.println((String) it.next()); // 输出 Set 集合中的元素
}
System.out.println("学生数量:" + set.size());
//
System.out.println("学生" + name1 + "是否在集合中:" + set.contains(name1));
Set<String> set1 = new HashSet<String>(); // 创建一个空的 Set 集合
set1.add(name1);
set1.add("abc");
System.out.println(set.containsAll(set1));
// System.out.println(set.retainAll(set1));
// System.out.println(set);
set.addAll(set1);
System.out.println(set);
}
}
Map集合
public static void main(String[] args) {
Map<String, String> users = new HashMap<String, String>();
users.put("11", "李雨"); // 将学生信息键值对存储到Map中
users.put("22", "赵益");
users.put("33", "高原");
users.put("44", "戴文浩");
System.out.println("******** 学生列表 ********");
Iterator<String> it = users.keySet().iterator();
while (it.hasNext()) {
// 遍历 Map
Object key = it.next();
Object val = users.get(key);
System.out.println("学号:" + key + ",姓名:" + val);
}
Scanner input = new Scanner(System.in);
System.out.println("请输入要删除的学号:");
int num = input.nextInt();
input.close();
if (users.containsKey(String.valueOf(num))) { // 判断是否包含指定键
users.remove(String.valueOf(num)); // 如果包含就删除
} else {
System.out.println("该学生不存在!");
}
System.out.println("******** 学生列表 ********");
Iterator<Map.Entry<String,String>> entryIterator = users.entrySet().iterator();
while (entryIterator.hasNext()) {
Map.Entry<String,String> current = entryIterator.next();
Object key = current.getKey();
Object val =current.getValue();
System.out.println("学号:" + key + ",姓名:" + val);
}
}
}