ArrayList和LinkedList的使用
Link是接口,ArrayList和LinkedList是去实现这个接口,List接口没有LinkedList类的方法,如果需要LinkedList的方法就不能写成List list = new LinkedList(),只能LinkedList list = new LinkedList()
1.ArrayList的使用
1.1.ArrayList采用长度可变数组,分配连续空间,效率很高
1.2.ArrayList操作数据
代码如下:
package began;
import java.util.ArrayList;
public class list {
public static void main(String[] args) {
//定义集合对象
ArrayList list = new ArrayList();
//添加元素
list.add(1); //add(object boj)
list.add("你好");
//插入元素
list.add(1,1.5); add(int index,object obj)
//删除元素 remove(Object)
list.remove(0); remove(object obj)和remove(index)
//遍历打印集合
for (int i=0;i<list.size();i++){
System.out.println(list.get(i)); //get:下标访问数组
}
}
}
//判断是否存在指定元素:contains()
1.3实际应用场景:图书管理系统
1.3.1.主函数
package began;
import java.util.ArrayList;
import java.util.List;
public class BOOKbody {
public static void main(String[] args) {
//定义一个集合list保存多本书
ArrayList list = new ArrayList();
BOOK b1=new BOOK("小王子",30,"圣埃克苏佩里");
BOOK b2=new BOOK("阿泰勒的角落",50,"李娟");
//加入书籍
list.add(b1);
list.add(b2);
//插入书籍
BOOK b3=new BOOK("平凡的世界",90,"路遥");
list.add(1,b3);
//删除元素
list.remove(b1);
list.remove(0);
//查询元素
if(list.contains(b2)){
System.out.println("存在该图书");
}else{
System.out.println("不存在该书");
}
for (int i=0;i<list.size();i++){
System.out.println(list.get(i));
//两种打印方法皆可
BOOK b=(BOOK)list.get(i);//将list.get(i)强制转为BOOK类型
System.out.println(b); //get:下标访问数组
}
}
}
1.3.1副函数
package began;
public class BOOK {
//类的属性
private String name;
private int price;
private String author;
//===============构造==============
public BOOK(String name, int price, String author) {
this.name = name;
this.price = price;
this.author = author;
}
//========重写toString() show=======
@Override
public String toString() {
return "BOOK{" +
"name='" + name + '\'' +
", price=" + price +
", author='" + author + '\'' +
'}';
}
//===============封装=================
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getPrice() {
return price;
}
public void setPrice(int price) {
this.price = price;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
}
2.LinkedList的使用
2.1.主函数
list.getLast();//返回列表最后一个元素
list.First();//返回列表第一个元素
图书管理系统示例代码:
package began;
import java.util.LinkedList;
public class BOOKbodylinkedlist {
public static void main(String[] args) {
//LinkedList:链表-->数据区+指针区
LinkedList list = new LinkedList(); //List接口没有LinkedList类的方法,不能写成List list = new LinkedList()
BOOKlinkedlist b1=new BOOKlinkedlist("小王子",30,"圣埃克苏佩里");
list.add(b1);
BOOKlinkedlist b2=new BOOKlinkedlist("阿泰勒的角落",50,"李娟");
BOOKlinkedlist b3=new BOOKlinkedlist("平凡的世界",90,"路遥");
list.addFirst(b2);
list.addLast(b3);
//获取第一本书
BOOKlinkedlist first =(BOOKlinkedlist) list.getFirst();
System.out.println(first);
System.out.println("==============");
//获取最后一本书
BOOKlinkedlist last =(BOOKlinkedlist) list.getLast();//返回列表最后一个元素
System.out.println(last);
System.out.println("==============");
//删除
list.removeFirst();
list.removeLast();
//输出
for (int i=0;i<list.size();i++){
BOOKlinkedlist b=(BOOKlinkedlist)list.get(i);
System.out.println(b);
}
}
}
2.2.副函数(与上边一样)
package began;
public class BOOKlinkedlist {
//类的属性
private String name;
private int price;
private String author;
//===============构造==============
public BOOKlinkedlist(String name, int price, String author) {
this.name = name;
this.price = price;
this.author = author;
}
//========重写toString() show=======
@Override
public String toString() {
return "BOOK{" +
"name='" + name + '\'' +
", price=" + price +
", author='" + author + '\'' +
'}';
}
//===============封装=================
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getPrice() {
return price;
}
public void setPrice(int price) {
this.price = price;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
}
标签:java,String,author,ArrayList,list,name,price,public,LinkedList
From: https://blog.csdn.net/2303_80933038/article/details/142282323