为什么有集合?
自动扩容
数组:长度固定,可以存基本数据类型和引用数据类型
集合:长度可变,可以存引用数据类型,基本数据类型的话需要包装类
ArrayList
public class studentTest {
public static void main(String[] args) {
ArrayList<String> list=new ArrayList<>();
boolean res=list.add("aaa");
System.out.println(res); //true
System.out.println(list);//[aaa]
list.add("bbb");
list.add("cccc");
System.out.println(list); //[aaa, bbb, cccc]
//删除
boolean res1=list.remove("aaa");
System.out.println(res1); //true
System.out.println(list);//[bbb, cccc]
//根据索引删除,返回被删除元素
String str=list.remove(0);
System.out.println(str);//bbb
//修改,返回旧值
String res2=list.set(0,"000");
System.out.println(res2);//cccc
System.out.println(list);//[000]
//查询
String str3=list.get(0);
System.out.println(str3);//000
//获取长度
System.out.println(list.size());//1
}
}
案例 创建集合,添加元素,遍历
public class studentTest {
public static void main(String[] args) {
ArrayList<String> list=new ArrayList<>();
list.add("bhjhsd");
list.add("dwshv");
list.add("fs2552");
System.out.print("[");
for (int i = 0; i <list.size() ; i++) {
if(i==list.size()-1){
System.out.println(list.get(i)+"]");
}else{
System.out.print(list.get(i)+", ");
}
}
}
}
案例 定义一个集合 添加数字,遍历。
包装类
public class studentTest {
public static void main(String[] args) {
ArrayList<Integer> list=new ArrayList<>();
list.add(1);
list.add(3);
list.add(5);
System.out.print("[");
for (int i = 0; i <list.size() ; i++) {
if(i==list.size()-1){
System.out.println(list.get(i)+"]");
}else{
System.out.print(list.get(i)+", ");
}
}
//[1, 3, 5]
}
}
案例 添加学生类对象
//存储学生信息
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 studentTest {
public static void main(String[] args) {
ArrayList<Student> list=new ArrayList<>();
Student s1=new Student("lisa",15);
Student s2=new Student("bob",18);
Student s3=new Student("anna",17);
list.add(s1);
list.add(s2);
list.add(s3);
for (int i = 0; i < list.size(); i++) {
Student stu=list.get(i);
System.out.println(stu.getName()+", "+stu.getAge());
}
}
//lisa, 15
//bob, 18
//anna, 17
}
键盘录入学生信息:
public class studentTest {
public static void main(String[] args) {
ArrayList<Student> list=new ArrayList<>();
Scanner sc=new Scanner(System.in);
for (int i = 0; i <3 ; i++) {
Student stu =new Student();
System.out.println("输入学生姓名");
String name=sc.next();
System.out.println("输入学生年龄");
int age=sc.nextInt();
stu.setAge(age);
stu.setName(name);
list.add(stu);
}
for (int i = 0; i <list.size() ; i++) {
Student s=list.get(i);
System.out.println(s.getName()+", "+s.getAge());
}
//hbkjsd, 12
//dv, 15
//dvg, 13
}
}
案例 添加用户对象并判断是否存在
public class User {
private String id;
private String username;
private String password;
public User() {
}
public User(String id, String username, String password) {
this.id = id;
this.username = username;
this.password = password;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
public class userTest {
public static void main(String[] args) {
ArrayList<User> list =new ArrayList<>();
User u1=new User("001","zhangsan","123456");
User u2=new User("002","lisi","12354526");
User u3=new User("003","wangwu","123dfssdf6");
list.add(u1);
list.add(u2);
list.add(u3);
System.out.println(contain(list,"002"));
}
public static boolean contain(ArrayList<User> list,String id){
for (int i = 0; i <list.size() ; i++) {
boolean res=list.get(i).getId().equals(id);
if(res){
return true;
}
}
return false;
}
}
更改,存在返回索引,不存在返回-1
public class userTest {
public static void main(String[] args) {
ArrayList<User> list =new ArrayList<>();
User u1=new User("001","zhangsan","123456");
User u2=new User("002","lisi","12354526");
User u3=new User("003","wangwu","123dfssdf6");
list.add(u1);
list.add(u2);
list.add(u3);
System.out.println(contain(list,"002"));
}
public static int contain(ArrayList<User> list,String id){
for (int i = 0; i <list.size() ; i++) {
boolean res=list.get(i).getId().equals(id);
if(res){
return i;
}
}
return -1;
}
}
案例 创建手机对象,低于价格的返回
//把价格低于3000的返回
public class phoneTest {
public static void main(String[] args) {
ArrayList<Phone> list =new ArrayList<>();
Phone p1=new Phone("xiaomi",1000);
Phone p2=new Phone("pingguo",8000);
Phone p3=new Phone("xchuizi",2999);
list.add(p1);
list.add(p2);
list.add(p3);
ArrayList<Phone> reslist=getPhoneInfo(list);
for (int i = 0; i < reslist.size(); i++) {
Phone p=reslist.get(i);
System.out.println(p.getBrand()+", "+p.getPrice());
}
}
public static ArrayList<Phone> getPhoneInfo(ArrayList<Phone> list){
ArrayList<Phone> reslist=new ArrayList<>();
for (int i = 0; i < list.size(); i++) {
Phone p=list.get(i);
int price=p.getPrice();
if(price<3000){
reslist.add(p);
}
}
return reslist;
}
}
本站资源均来自互联网,仅供研究学习,禁止违法使用和商用,产生法律纠纷本站概不负责!如果侵犯了您的权益请与我们联系!
转载请注明出处: 免费源码网-免费的源码资源网站 » java学习-集合
发表评论 取消回复