List实现ArrayList
ArrayList是List接口最常用的一个实现类,支持List接口的一些列操作。
创建格式:
ArrayList<数据类型> = new ArrayList<>();
常用方法:
![](https://i-blog.csdnimg.cn/blog_migrate/c5edc824e1f8559de5a9b8b4762d577e.png)
集合遍历:
![](https://i-blog.csdnimg.cn/blog_migrate/aadffe82cab91b293677d8c0d20af33c.png)
四道练习:
1.
![](https://i-blog.csdnimg.cn/blog_migrate/fd9f0127d666b7a4e527d287262a5a34.png)
import java.util.ArrayList;
import java.util.Scanner;
public class ArrayList_Test {
public static void main(String[] args){
ArrayList <String> AL = new ArrayList<>();
AL.add("hello!");
AL.add("Stephen!");
AL.add("Curry!");
System.out.print("[");
for(int i = 0;i<AL.size();i++){
if(i==AL.size()-1){
System.out.print(AL.get(i));
}else{
System.out.print(AL.get(i)+", ");
}
}
System.out.print("]");
}
}
2.
![](https://i-blog.csdnimg.cn/blog_migrate/8d05cc889a2b53b2186ee016d95492e5.png)
import java.util.ArrayList;
import java.util.Scanner;
import java.util.stream.StreamSupport;
class Student {
private String name;
private int number;
public Student() {
}
public Student(String name, int number) {
this.name = name;
this.number = number;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getNumber() {
return number;
}
public void setNumber(int number) {
this.number = number;
}
}
public class ArrayList_Test {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
ArrayList<Student> AL = new ArrayList<>(3); //创建集合
for(int i = 0;i<3;i++){
Student s = new Student();
System.out.println("请输入姓名:");
String name = sc.next();
System.out.println("请输入号码:");
int number = sc.nextInt();
s.setName(name);
s.setNumber(number);
AL.add(s);
}
for(int i =0;i<AL.size();i++){
Student stu = AL.get(i);
System.out.println(stu.getName()+"\t"+stu.getNumber());
}
}
}
3.
![](https://i-blog.csdnimg.cn/blog_migrate/27bdac722e59dd2a7346b75c96053dcc.png)
import java.util.ArrayList;
import java.util.Scanner;
class Users{
private String id;
private String name;
private String password;
public Users() {
}
public Users(String id, String name, String password) {
this.id = id;
this.name = name;
this.password = password;
}
/**
* 获取
* @return id
*/
public String getId() {
return id;
}
/**
* 设置
* @param id
*/
public void setId(String id) {
this.id = id;
}
/**
* 获取
* @return name
*/
public String getName() {
return name;
}
/**
* 设置
* @param name
*/
public void setName(String name) {
this.name = name;
}
/**
* 获取
* @return password
*/
public String getPassword() {
return password;
}
/**
* 设置
* @param password
*/
public void setPassword(String password) {
this.password = password;
}
}
public class ArrayList_Test {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
ArrayList<Users> AL = new ArrayList<>();
for (int i = 0; i < 3; i++) {
Users u = new Users();
System.out.println("请输入id:");
String id = sc.next();
System.out.println("请输入姓名:");
String name = sc.next();
System.out.println("请输入密码:");
String password = sc.next();
u.setId(id);
u.setName(name);
u.setPassword(password);
AL.add(u);
}
System.out.println("请输入需要查找的id:");
String id1 = sc.next();
System.out.println("查找结果为:");
System.out.println(judge(id1,AL));
}
public static boolean judge(String id,ArrayList<Users> AL){
for(int i = 0;i<AL.size();i++){
if(AL.get(i).getId().equals(id)){
return true;
}
}
return false;
}
}
4.
![](https://i-blog.csdnimg.cn/blog_migrate/5b8ae5ef561b52994d707d5c782cec5a.png)
import java.util.ArrayList;
class Phone{
private String brand;
private int price;
public Phone() {
}
public Phone(String brand, int price) {
this.brand = brand;
this.price = price;
}
public String getBrand() {
return brand;
}
public void setBrand(String brand) {
this.brand = brand;
}
public int getPrice() {
return price;
}
public void setPrice(int price) {
this.price = price;
}
}
public class ArrayList_Test{
public static void main(String [] args){
ArrayList<Phone> AL = new ArrayList<>();
Phone phone1 = new Phone("小米",1000);
Phone phone2 = new Phone("苹果",8000);
Phone phone3 = new Phone("锤子",2999);
AL.add(phone1);
AL.add(phone2);
AL.add(phone3);
ArrayList<Phone> resultAL = find(AL);
for(int i =0;i<resultAL.size();i++){
System.out.println(resultAL.get(i).getBrand()+"\t"+resultAL.get(i).getPrice());
}
}
public static ArrayList<Phone> find(ArrayList<Phone> AL){ //要返回多个量时,可以返回一个集合或者数组
ArrayList<Phone> resultAL = new ArrayList<>();
for(int i = 0;i<AL.size();i++){
if(AL.get(i).getPrice()<3000){
resultAL.add(AL.get(i));
}
}
return resultAL;
}
}
总结一下^_^
这两天简单卷了一下集合里ArrayList的相关内容,区别了和数组的不同点,也实际练习了一些!
![](https://i-blog.csdnimg.cn/blog_migrate/3ff488447ef2301b4e1e82969df66be2.jpeg)