ArrayList
test类
package work;
import java.util.ArrayList;
import java.util.Scanner;
public class test {
public static void main(String[] args) {
ArrayList
Scanner sc=new Scanner(System.in);
for(int i=0;i<3;i++){
Student s = new Student();
System.out.println("请输入姓名");
String name =sc.next();
System.out.println("请输入年龄");
int age =sc.nextInt();
s.setAge(age);
s.setName(name);
list.add(s);
}
for(int i=0;i<list.size();i++){
Student stu=list.get(i);
System.out.println(stu.getAge()+" "+stu.getName());
}
}
}
Student封装类
package work;
public class Student {
private int age;
private String name;
public Student() {
}
public Student(int age, String name) {
this.age = age;
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
ArrayList作为一个容器 存储Student类里的成员 最后输出还要转为Student类
返回多个数据
test2类
package work;
import java.util.ArrayList;
public class test2 {
public static void main(String[] args) {
ArrayList
Phone p1=new Phone("小米",1000);
Phone p2=new Phone("苹果",8000);
Phone p3=new Phone("锤子",2999);
list.add(p1);
list.add(p2);
list.add(p3);
ArrayList<Phone> Phonelist=getPhone(list);
for (int i = 0; i < Phonelist.size(); i++) {
Phone P=Phonelist.get(i);
System.out.println(P.getBrand()+","+P.getPrice());
}
}
public static ArrayList<Phone> getPhone(ArrayList<Phone> list){
ArrayList<Phone> phoneList=new ArrayList<>();
for(int i=0;i<list.size();i++){
Phone p=list.get(i);
if(p.getPrice()<3000){
phoneList.add(p);
}
}
return phoneList;
}
}
java bean封装Phoen
package work;
public 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;
}
}
标签:光棍节,String,int,ArrayList,11.11,Phone,public,name From: https://www.cnblogs.com/yzl168324/p/18540640