目录
一、整体架构图
二、List集合类(有序的,可重复的)
1.顺序列表ArrayList
ArrayList实现了List的接口,是基于可变长度属组的列表实现
-
import java.util.ArrayList;
-
import java.util.Collections;
-
import java.util.Iterator;
-
import java.util.List;
-
-
public
class
y2 {
-
public
static
void
main
(String[] args) {
-
//定义方法
-
List<String> list =
new
ArrayList<String>();
-
//添加元素
-
list.add(
"aaa");
-
list.add(
"bbb");
-
list.add(
"ccc");
-
//遍历方法1,使用size与get方法
-
for (
int i=
0;i<list.size();i++){
-
System.out.println(list.get(i));
-
}
-
//删除下标为1的元素
-
list.remove(
1);
-
//遍历方法2:采用加强for循环的方法
-
for (String s:list){
-
System.out.println(s);
-
}
-
//获得下标为1的元素
-
System.out.println(list.get(
1));
-
//遍历方法3:使用Iterator接口
-
Iterator<String> iterator = list.iterator();
-
//Iterator主要方法:
-
//hasNext()用来判断是否还有下一个元素
-
//next()用来获得下一个元素
-
//remove用来删除当前元素
-
while(iterator.hasNext()){
-
System.out.println(iterator.next());
-
}
-
//List集合排序
-
Collections.sort(list);
-
}
-
}
2.链式列表LinkedList
LinkedList也实现了List接口,其实质是基于指针(链)的列表实现
-
import java.util.*;
-
public
class
y2 {
-
public
static
void
main
(String[] args) {
-
//定义方法
-
LinkedList<String> list =
new
LinkedList<String>();
-
//添加元素
-
list.add(
"aaa");
-
list.add(
"bbb");
-
list.add(
"ccc");
-
//向列表头插入一个元素
-
list.addFirst(
"zzm");
-
//向列表尾插入一个元素
-
list.addLast(
"mzm");
-
-
//遍历方法1,使用size与get方法
-
for (
int i=
0;i<list.size();i++){
-
System.out.println(list.get(i));
-
}
-
System.out.println(
"---------------");
-
//删除下标为1的元素
-
list.remove(
1);
-
//删除列表头的元素
-
list.removeFirst();
-
//删除列表尾的元素
-
list.removeLast();
-
//遍历方法2:采用加强for循环的方法
-
for (String s:list){
-
System.out.println(s);
-
}
-
System.out.println(
"---------------");
-
//获得下标为1的元素
-
System.out.println(list.get(
1));
-
//获得列表头元素
-
System.out.println(list.getFirst());
-
//获得列表尾元素
-
System.out.println(list.getLast());
-
-
System.out.println(
"---------------");
-
//遍历方法3:使用Iterator接口
-
Iterator<String> iterator = list.iterator();
-
//Iterator主要方法:
-
//hasNext()用来判断是否还有下一个元素
-
//next()用来获得下一个元素
-
//remove用来删除当前元素
-
while(iterator.hasNext()){
-
System.out.println(iterator.next());
-
}
-
//List集合排序
-
Collections.sort(list);
-
}
-
}
三、Set集合类(不可重复)
1.HashSet(哈希集合)
HashSet实现了Set接口,并且不保证元素的迭代顺序,对其进行添加、删除元素等操作的时间复杂度是常量级的。
-
import java.util.HashSet;
-
import java.util.Set;
-
-
public
class
y3 {
-
public
static
void
main
(String[] args) {
-
//定义
-
Set<String> hashSet =
new
HashSet<String>();
-
//添加元素
-
hashSet.add(
"aaa");
-
hashSet.add(
"bbb");
-
hashSet.add(
"ccc");
-
//遍历结果是无序的
-
System.out.println(hashSet);
-
}
-
}
2.LinkedHashSet(链式哈希集合)
LinkedHashSet继承与HashSet,其也是根据元素的哈希码来决定元素的存储位置。
-
import java.util.LinkedHashSet;
-
import java.util.Set;
-
-
public
class
y3 {
-
public
static
void
main
(String[] args) {
-
//定义
-
Set<String> linkedHashSet =
new
LinkedHashSet<String>();
-
//添加元素
-
linkedHashSet.add(
"aaa");
-
linkedHashSet.add(
"bbb");
-
linkedHashSet.add(
"ccc");
-
linkedHashSet.add(
"zzm");
-
//遍历结果是无序的
-
System.out.println(linkedHashSet);
-
}
-
}
3.TreeSet(树形集合)
TreeSet采用树形结构来存取集合元素(输出为有序,默认升序)
-
import java.util.Comparator;
-
import java.util.Set;
-
import java.util.TreeSet;
-
-
public
class
y3 {
-
public
static
void
main
(String[] args) {
-
//定义
-
Set<String> treeSet =
new
TreeSet<String>();
-
//添加元素
-
treeSet.add(
"aaa");
-
treeSet.add(
"ddd");
-
treeSet.add(
"ccc");
-
//遍历结果是有序的(默认为升序)
-
for (String s:treeSet){
-
System.out.println(s);
-
}
-
-
//创建TreeSet时指定了比较器(覆盖了String默认比较规则)
-
Set<String> treeSet2 =
new
TreeSet<String>(
new
Comparator<String>() {
-
@Override
-
public
int
compare
(String o1, String o2) {
-
return o2.length()-o1.length();
-
}
-
});
-
treeSet2.add(
"abcde");
-
treeSet2.add(
"zed");
-
treeSet2.add(
"abcd");
-
//按照字符串长度降序排列字符串
-
for (String s2:treeSet2){
-
System.out.println(s2);
-
}
-
}
-
}
四、Map集合类(无序,键唯一,值不唯一)
-
import java.util.*;
-
-
public
class
y4{
-
public
static
void
print
(Map<Integer,String> map) {
-
map.put(
1,
"张三");
-
map.put(
3,
"王五");
-
map.put(
2,
"李四");
-
//获取map对象中键的集合
-
Set<Integer> set = map.keySet();
-
//遍历这个map
-
for (Integer key:set){
-
System.out.println(key+
": "+map.get(key));
-
}
-
}
-
public
static
void
main
(String[] args) {
-
//HashMap存放的次序
-
print(
new
HashMap<Integer,String>());
-
//LinkedHashMap存放次序
-
print(
new
LinkedHashMap<Integer,String>());
-
//TreeSet存放次序
-
print(
new
TreeMap<Integer,String>());
-
}
-
-
}
由运行结果得知,HashMap的遍历结果是无序的,LinkedHashMap是按照加入顺序遍历,TreeMap是按照值升序遍历
五、数组
-
import java.util.Arrays;
-
import java.util.Collections;
-
import java.util.Scanner;
-
-
public
class
y5 {
-
public
static
void
main
(String[] args) {
-
Scanner
sc
=
new
Scanner(System.in);
-
//定义数组
-
int[] a =
new
int[
10];
-
//为一个数组赋初值
-
for (
int i=
0;i<
10;i++){
-
a[i] = sc.nextInt();
-
}
-
//数组遍历
-
for (
int s:a){
-
System.out.print(s+
" ");
-
}
-
System.out.println();
-
//数组排序(默认为升序)
-
Arrays.sort(a);
-
for (
int s:a){
-
System.out.print(s+
" ");
-
}
-
System.out.println();
-
//数组降序排列(需要包装类型数组)
-
Integer[] integers =
new
Integer[
5];
-
for (
int i=
0;i<
5;i++){
-
integers[i] = sc.nextInt();
-
}
-
Arrays.sort(integers, Collections.reverseOrder());
-
for (Integer s:integers){
-
System.out.print(s+
" ");
-
}
-
}
-
}