集合
Collection集合使用
1.基本使用
public class Demo01 {
public static void main(String[] args) {
//创建对象
Collection<String> c = new ArrayList<>();
//添加元素
c.add("hello");
c.add("world");
c.add("java");
//输出集合
System.out.println(c);
}
}
里面的String为集合的泛型
2.常用方法
3.遍历
public class Demo02 {
public static void main(String[] args) {
//创建对象
Collection<String> c = new ArrayList<>();
//添加元素
c.add("chen");
c.add("c");
c.add("che");
c.add("ch");
//返回集合中元素的迭代器
Iterator<String> it = c.iterator();
//while循环改进
while (it.hasNext()) { //hasnext判断迭代有无元素
String s = it.next(); //next 获取返回迭代中下一个元素
System.out.println(s);
//System.out.printIn(it.next())
}
}
}
4.应用
创建一个存储学生对象的集合,存储3个学生对象,使用程序实现在控制台遍历该集合
public class Student {
private String name;
private int age;
public Student() {
}
public Student(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
public class CollectionDemo {
public static void main(String[] args) {
//创建Collection对象
Collection<Student> c = new ArrayList<Student>();
//创建学生对象
Student s1 = new Student("赵云",20);
Student s2 = new Student("关羽",22);
Student s3 = new Student("张飞",23);
//把学生添加到集合
c.add(s1);
c.add(s2);
c.add(s3);
//遍历集合
Iterator<Student> it = c.iterator();
while (it.hasNext()) {
Student s = it.next();
System.out.println(s.getName()+ "," + s.getAge());
}
}
}
ArrayList
可变的存储类型
ArrayList:可调整的数组类型
ArrayList
使用:
方法名 | 说明 |
---|---|
ArrayList 集合名 = new ArrayList(); | 创建空的集合对象 |
集合名.add() | 将指定元素追加到集合末尾 |
集合名.add(int index,E element) | 指定位置插入指定元素 |
代码:
public class Demo01 {
public static void main(String[] args) {
//ArrayList<String> array = new ArrayList<>();与下方一样,创建空集合
ArrayList<String> array = new ArrayList<String >();
array.add("hello");//依次追加到末尾
array.add("world");
array.add("hi");
array.add(1,"cs");//指定位置插入元素,可以是最后一个,但不能越界。
System.out.println(array);
}
}
常用方法:
方法名 | 说明 |
---|---|
集合名.remove | 删除指定元素,输出返回是否删除成功 |
集合名.remove | 删除指定索引元素,输出返回被删除的元素 |
集合名.set(索引,元素) | 修改指定索引元素,输出返回被修改的元素 |
get | 输出返回指定索引处的元素 |
size | 输出返回集合中元素的个数 |
代码:
public class Demo02 {
public static void main(String[] args) {
ArrayList<String> array = new ArrayList<>();
array.add("c");
array.add("s");
array.add("y");
array.add("s");
//array.remove(1);//删除索引的元素,不能越界
//array.remove("c");删除c这个元素
//array.set(1,"java");//修改指定索引的元素,不能索引越界
System.out.println(array.get(0));//返回索引位置元素,不能索引越界
System.out.println(array);
System.out.println(array.size());//返回元素个数
}
}
遍历:
for(int i = 0;i<集合对象.size();i++) {
集合对象.get(i);
}
for (int i = 0; i < array.size(); i++) {
System.out.println(array.get(i));
}
LIst
public class Student {
// list集合存储三个学生对象,并遍历
// 1.创建学生类
private String name;
private int age;
// 2.创建list集合
public Student() {
}
public Student(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
// 3.将学生类存储到list中
// 4.利用Iterator遍历
}
public class ListTest {
public static void main(String[] args) {
List<Student> s = new ArrayList<Student>();
Student s1 = new Student("赵云", 23);
Student s2 = new Student("赵", 25);
Student s3 = new Student("赵云自", 24);
s.add(s1);
s.add(s2);
s.add(s3);
Iterator<Student> b = s.iterator();
while (b.hasNext()) {
Student sb = b.next();
System.out.println(sb.getAge()+","+ sb.getName());
}
}
}
ListIterator方法
public class ListIteratorDemo {
public static void main(String[] args) {
//创建list集合 添加对象 listIterator
ArrayList<String> list = new ArrayList<>();
list.add("cs");
list.add("dd");
list.add("cc");
list.add("ss");
ListIterator<String> lis = list.listIterator();
while (lis.hasNext()) {
String s = lis.next();
if (s.equals("cc")) {
lis.add("javase");
}
}
System.out.println(list);
}
}
三种for循环
public class Demo01 {
public static void main(String[] args) {
ArrayList<Student> s = new ArrayList<>();
Student s1 = new Student("赵云", 23);
Student s2 = new Student("赵", 25);
Student s3 = new Student("赵云自", 24);
s.add(s1);
s.add(s2);
s.add(s3);
//iterator
Iterator<Student> it = s.iterator();
while (it.hasNext()) {
Student b = it.next();
System.out.println(b.getAge()+ "," + b.getName());
}
//for循环
for (int i = 0; i < s.size(); i++) {
Student b = s.get(i);
System.out.println(b.getAge() + "," +b.getName());
}
//增强for循环
for (Student student : s) {
System.out.println(student.getAge()+"," +student.getAge());
}
}
}
数据结构
数据栈
数据进入栈,压/进栈
栈出数据,弹/出栈
先进后出
队列
先进先出
数组
查询快,增删慢
链表结构
查询慢,增删快
ArrayList 集合
底层是数组结构实现,查询快、增删慢
LinkedList 集合
底层是链表结构实现,查询慢、增删快
LinkedList集合
public class LinkedListDemo {
public static void main(String[] args) {
LinkedList<String> linkedList = new LinkedList<String>();
linkedList.add("cs");
linkedList.add("csss");
linkedList.add("css");
linkedList.add("we");
linkedList.addFirst("wee");
linkedList.addLast("weee");
System.out.println(linkedList);
System.out.println(linkedList.getFirst());
System.out.println(linkedList.getLast());
System.out.println(linkedList.removeFirst());
System.out.println(linkedList.removeLast());
System.out.println(linkedList);
}
}