1.ArrayList类 用法
package cn.nwafu.ArrayList;
import java.util.ArrayList;
public class test2 {
public static void main(String[] args) {
//1.创建一个集合
ArrayList<String> list = new ArrayList<>();
//2.添加元素
list.add("aaa");
list.add("bbb");
list.add("ccc");
list.add("ddd");
System.out.println(list);//[aaa, bbb, ccc, ddd]
//3.删除元素
boolean result1 = list.remove("aaa");
System.out.println(result1);//true
System.out.println(list);//[bbb, ccc, ddd]
boolean result2 = list.remove("abc");
System.out.println(result2);//false
String str = list.remove(2);
System.out.println(str);//ddd(被删除的元素)
System.out.println(list);//[bbb, ccc]
//4.修改元素
String result3 =list.set(1,"ddd");
System.out.println(result3);//ccc(被修改的元素)
System.out.println(list);//[bbb, ddd]
//5.查询元素
String s = list.get(1);
System.out.println(s);//ddd
//遍历
for (int i = 0; i < list.size(); i++) {
String everyStr = list.get(i);
System.out.print(everyStr+" ");//bbb ddd
}
}
}
练习1.集合的遍历方式
需求:定义一个集合,添加字符串,并进行遍历
遍历格式参照:[元素1,元素2,元素3]。
package cn.nwafu.ArrayList;
import java.util.ArrayList;
public class test3 {
public static void main(String[] args) {
ArrayList<String> list = new ArrayList<>();
list.add("今天你吃饭了么?");
list.add("今天你睡觉了么?");
list.add("今天你微笑了么?");
list.add("今天你努力了么?");
System.out.print("[");
for (int i = 0; i < list.size(); i++) {
if ( i == list.size()-1){
System.out.print(list.get(i));
}else{
System.out.print(list.get(i)+",");
}
}
System.out.println("]");
}
}
练习2.添加数字并遍历
需求:定义一个集合,添加数字,并进行遍历。
遍历格式参照:[元素1,元素2,元素3]。
package cn.nwafu.ArrayList;
import java.util.ArrayList;
public class test4 {
public static void main(String[] args) {
// 练习2.添加数字并遍历
// 需求:定义一个集合,添加数字,并进行遍历。
// 遍历格式参照:[元素1,元素2,元素3]。
ArrayList<Integer> list = new ArrayList<>();
//jdk5以后,int Integer 之间可以相互转化的
list.add(1);
list.add(2);
list.add(3);
list.add(4);
list.add(5);
System.out.print("[");
for (int i = 0; i < list.size(); i++) {
if ( i == list.size()-1){
System.out.print(list.get(i));
}else{
System.out.print(list.get(i)+",");
}
}
System.out.println("]");
}
}
练习3.添加学生对象并遍历
需求:定义一个集合,添加一些学生对象,并进行遍历
学生类的属性为:姓名,年龄。
Student.Java
package cn.nwafu.ArrayList;
public class Student {
private String name;
private int age;
public Student() {
}
public Student(String name, int age) {
this.name = name;
this.age = age;
}
/**
* 获取
* @return name
*/
public String getName() {
return name;
}
/**
* 设置
* @param name
*/
public void setName(String name) {
this.name = name;
}
/**
* 获取
* @return age
*/
public int getAge() {
return age;
}
/**
* 设置
* @param age
*/
public void setAge(int age) {
this.age = age;
}
}
test5.Java
package cn.nwafu.ArrayList;
import java.util.ArrayList;
public class test5 {
public static void main(String[] args) {
ArrayList<Student> list = new ArrayList<>();
Student s1 = new Student("张三",23);
Student s2 = new Student("李四",27);
Student s3 = new Student("王五",25);
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());
}
}
}
练习4.添加学生对象并遍历
需求:定义一个集合,添加一些学生对象,并进行遍历
学生类的属性为:姓名,年龄。
要求:对象的数据来自键盘录入
Student.Java 类与上一个练习一样,在这里不重复了!
test6.Java
package cn.nwafu.ArrayList;
import java.util.ArrayList;
import java.util.Scanner;
public class test6 {
public static void main(String[] args) {
ArrayList<Student> list = new ArrayList<>();
for (int i = 0; i < 3; i++) {//此时i应该小于你要添加对象的的个数,而不是list.size(),
//因为刚开始list的长度可变,起始为0,进不去这个循环
Scanner sc = new Scanner(System.in);//*************看下面图片注释,必须放到循环的里面
Student s = new Student();
System.out.println("请输入学生的姓名");
String name = sc.next();
System.out.println("请输入学生的年龄");
int age = sc.nextInt();
//把name和age赋值给学生对象
s.setName(name);
s.setAge(age);
//把学生对象添加到集合当中
list.add(s);
}
System.out.println(list.size());
for (int i = 0; i < list.size(); i++) {
Student stu = list.get(i);
System.out.println(stu.getName()+","+stu.getAge());
}
}
}
上述代码中Scanner sc = new Scanner(System.in);语句的注释
练习5.添加用户对象并判断是否存在
需求:
1, main方法中定义一个集合,存入三个用户对象。
用户属性为:id,username, password
2,要求:定义一个方法,根据id查找对应的用户信息。
如果存在,返回true
如果不存在,返回false
User.Java
package cn.nwafu.ArrayList;
public class User {
private String id;
private String name;
private String password;
public User() {
}
public User(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;
}
}
test7.Java
package cn.nwafu.ArrayList;
import java.util.ArrayList;
public class test7 {
public static void main(String[] args) {
// 练习5.添加用户对象并判断是否存在
// 需求:
// 1, main方法中定义一个集合,存入三个用户对象。
// 用户属性为:id,username, password
// 2,要求:定义一个方法,根据id查找对应的用户信息。
// 如果存在,返回true
// 如果不存在,返回false
ArrayList<User> list = new ArrayList<>();
User u1 = new User("heima001","zhangsan","123456");
User u2 = new User("heima002","lisi","12345696");
User u3 = new User("heima003","wangwu","88888888");
list.add(u1);
list.add(u2);
list.add(u3);
boolean flag = contains(list,"heima001");
System.out.println(flag);
}
public static boolean contains(ArrayList<User> list ,String id){
for (int i = 0; i < list.size(); i++) {
if(list.get(i).getId().equals(id)){
return true;
}
}
return false;
}
}
练习5变形题.
需求:
1,main方法中定义一个集合,存入三个用户对象。
用户属性为: id,username,password
2,要求:定义一个方法,根据id查找对应的用户信息。
如果存在,返回索引
如果不存在,返回-1
User.Java 与上面的代码一样,不做重复!
test8.Java
package cn.nwafu.ArrayList;
import java.util.ArrayList;
public class test8 {
public static void main(String[] args) {
// 练习5变形题.
// 需求:
// 1,main方法中定义一个集合,存入三个用户对象。
// 用户属性为: id,username,password
// 2,要求:定义一个方法,根据id查找对应的用户信息。
// 如果存在,返回索引
// 如果不存在,返回-1
ArrayList<User> list = new ArrayList<>();
User u1 = new User("heima001","zhangsan","123456");
User u2 = new User("heima002","lisi","12345696");
User u3 = new User("heima003","wangwu","88888888");
list.add(u1);
list.add(u2);
list.add(u3);
int index = getIndex(list,"heima003");
System.out.println(index);
}
public static int getIndex(ArrayList<User> list ,String id){
for (int i = 0; i < list.size(); i++) {
if(list.get(i).getId().equals(id)){
return i;
}
}
return -1;
}
}
如果既要返回是否存在,又要返回索引值,可以将两个代码合并成下面的代码
User.Java 与上面的代码一样,不做重复!
test8.Java
package cn.nwafu.ArrayList;
import java.util.ArrayList;
public class test8 {
public static void main(String[] args) {
// 练习5变形题.
// 需求:
// 1,main方法中定义一个集合,存入三个用户对象。
// 用户属性为: id,username,password
// 2,要求:定义一个方法,根据id查找对应的用户信息。
// 如果存在,返回索引
// 如果不存在,返回-1
ArrayList<User> list = new ArrayList<>();
User u1 = new User("heima001","zhangsan","123456");
User u2 = new User("heima002","lisi","12345696");
User u3 = new User("heima003","wangwu","88888888");
list.add(u1);
list.add(u2);
list.add(u3);
boolean flag = contains(list,"heima003");
System.out.println(flag);
int index = getIndex(list,"heima003");
System.out.println(index);
}
public static boolean contains(ArrayList<User> list ,String id) {
// for (int i = 0; i < list.size(); i++) {
// if (list.get(i).getId().equals(id)) {
// return true;
// }
// }
// return false;
return getIndex(list,id) >= 0;
}
public static int getIndex(ArrayList<User> list ,String id){
for (int i = 0; i < list.size(); i++) {
if(list.get(i).getId().equals(id)){
return i;
}
}
return -1;
}
}
练习6.添加手机对象并返回要求的数据
需求:
定义Javabean类:PhonePhone属性:品牌,价格。
main方法中定义一个集合,存入三个手机对象。
分别为:小米,1000。苹果,8000。锤子2999。
定义一个方法,将价格低于3000的手机信息返回。
Phone.Java
package cn.nwafu.ArrayList;
public class Phone {
private String brand;
private int price;
public Phone() {
}
public Phone(String brand, int price) {
this.brand = brand;
this.price = price;
}
/**
* 获取
* @return brand
*/
public String getBrand() {
return brand;
}
/**
* 设置
* @param brand
*/
public void setBrand(String brand) {
this.brand = brand;
}
/**
* 获取
* @return price
*/
public int getPrice() {
return price;
}
/**
* 设置
* @param price
*/
public void setPrice(int price) {
this.price = price;
}
}
test9.Java
package cn.nwafu.ArrayList;
import java.util.ArrayList;
public class test9 {
public static void main(String[] args) {
// 练习6.添加手机对象并返回要求的数据
// 需求:
// 定义Javabean类:PhonePhone属性:品牌,价格。
// main方法中定义一个集合,存入三个手机对象。
// 分别为:小米,1000。苹果,8000。锤子2999。
// 定义一个方法,将价格低于3000的手机信息返回。
ArrayList<Phone> list = new ArrayList<>();
Phone ph1 = new Phone("小米",1000);
Phone ph2 = new Phone("苹果",8000);
Phone ph3 = new Phone("锤子",2999);
list.add(ph1);
list.add(ph2);
list.add(ph3);
//遍历打印放在main方法中以便后续统计 修改价格 等等,更方便
ArrayList<Phone> resultList = getPhoneInfo(list);
for (int i = 0; i < resultList.size(); i++) {
Phone phone = resultList.get(i);
System.out.println(phone.getBrand()+"," +phone.getPrice());
}
}
//技巧:
//如果我们要返回多个数据,可以把这些数据先放到一个容器当中,再把容器返回
//集合 或者 数组 均可
public static ArrayList<Phone> getPhoneInfo(ArrayList<Phone> list){
ArrayList<Phone> resultList = new ArrayList<>();
for (int i = 0; i < list.size(); i++) {
Phone p = list.get(i);
int price =p.getPrice();
if (price < 3000){
resultList.add(p);
//System.out.println(p.getBrand()+","+p.getPrice());
}
}
return resultList;
}
}