集合和数组的对比
ArrayList成员方法
方法名 | 说明 |
---|---|
boolean add(E e) | 添加元素,返回值表示是否添加成功 |
boolean remove(E e) | 删除指定元素,返回值表示是否删除成功 |
E remove(int index) | 删除指定索引的元素,返回被删除元素 |
E set(int index,E e) | 修改指定索引下的元素,返回原来的元素 |
E get(int index) | 获取指定索引的元素 |
int size() | 集合的长度,也就是集合中元素的个数 |
基本数据类型 | 对应的包装类 |
---|---|
byte | Byte |
short | Short |
char | Character |
int | Integer |
long | Long |
float | Float |
double | Double |
boolean | Boolean |
需求:定义一个集合,添加字符串,并进行遍历
遍历格式参照:[元素1,元素2,元素3]。
package ArrayList;
import java.util.ArrayList;
public class Dome2 {
public static void main(String[] args) {
ArrayList<String> list1 = new ArrayList<>();
list1.add("健身了吗");
list1.add("健身了吗");
list1.add("健身了吗");
list1.add("健身了吗");
System.out.print("[");
for (int i = 0; i < list1.size(); i++) {
if(i==list1.size()-1){
System.out.print(list1.get(i));
}else {
System.out.print(list1.get(i)+",");
}
};
System.out.println("]");
}
}
需求:定义一个集合,添加一些学生对象,并进行遍历
学生类的属性为:姓名,年龄。
Student类
package ArrayList;
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 String toString() {
return "Student{name = " + name + ", age = " + age + "}";
}
}
main
package ArrayList;
import java.util.ArrayList;
public class Dome4 {
public static void main(String[] args) {
ArrayList<Student> list = new ArrayList<>();
Student s1 = new Student("libai",101);
Student s2 = new Student("dufu",102);
list.add(s1);
list.add(s2);
for (int i = 0; i < list.size(); i++) {
Student stu = list.get(i);
System.out.println(stu.toString());
}
}
}
需求:
true
false
User类
package 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;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String toString() {
return "User{id = " + id + ", name = " + name + ", password = " + password + "}";
}
}
main
package ArrayList;
import java.util.ArrayList;
public class Dome5 {
public static void main(String[] args) {
ArrayList<User> list = new ArrayList<>();
User u1 = new User("A001","zhangsan","123456");
User u2 = new User("A002","lisi","asdfgh");
User u3 = new User("A003","wangwu","123qwe");
list.add(u1);
list.add(u2);
list.add(u3);
System.out.println( panduan(list,"A001"));
}
public static boolean panduan(ArrayList<User> list,String id){
int flag = getIndex( list,id);
if(flag>-1){
return true;
}
return false;
}
public static int getIndex(ArrayList<User> list,String id){
for (int i = 0; i < list.size(); i++) {
if(list.get(i).getId()==id){
return i;
}
}
return -1;
}
}
定义Javabean类:Phone
Phone属性:品牌,价格。
main方法中定义一个集合,存入三个手机对象。
分别为∶小米,1000。苹果,8000。锤子2999。
定义一个方法,将价格低于3000的手机信息返回。
Phone类
package ArrayList;
public class Phone {
private String name;
private int price;
public Phone() {
}
public Phone(String name, int price) {
this.name = name;
this.price = price;
}
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 toString() {
return "Phone{name = " + name + ", price = " + price + "}";
}
}
main
package ArrayList;
import java.util.ArrayList;
public class Dome6 {
public static void main(String[] args) {
ArrayList<Phone> list = new ArrayList<>();
Phone p1 = new Phone("苹果",8999);
Phone p2 = new Phone("小米",1999);
Phone p3 = new Phone("锤子",2999);
list.add(p1);
list.add(p2);
list.add(p3);
ArrayList<Phone> newlist = getPhone(list);
for (int i = 0; i < newlist.size(); i++) {
System.out.println(newlist.get(i).getName()+","+newlist.get(i).getPrice());
}
}
public static ArrayList<Phone> getPhone(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);
}
}
return resultlist;
}
}
Student类
package StudentControlSystem;
public class Student {
private String id;
private String name;
private int age;
private String address;
public Student() {
}
public Student(String id, String name, int age, String address) {
this.id = id;
this.name = name;
this.age = age;
this.address = address;
}
/**
* 获取
* @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 age
*/
public int getAge() {
return age;
}
/**
* 设置
* @param age
*/
public void setAge(int age) {
this.age = age;
}
/**
* 获取
* @return address
*/
public String getAddress() {
return address;
}
/**
* 设置
* @param address
*/
public void setAddress(String address) {
this.address = address;
}
public String toString() {
return "Student{id = " + id + ", name = " + name + ", age = " + age + ", address = " + address + "}";
}
}
package StudentControlSystem;
import java.util.Scanner;
public class StudentSystem {
public static void main(String[] args) {
loop: while (true) {
System.out.println("-------------------------------靓仔,你来了--------------------------------------");
System.out.println("1:添加学生");
System.out.println("2:删除学生");
System.out.println("3:修改学生");
System.out.println("4:查询学生");
System.out.println("5:退出");
System.out.println("请输入您的选择:");
Scanner sc = new Scanner(System.in);
String choose = sc.next();
switch (choose){
case "1" -> addStudent();
case "2" -> deleteStudent();
case "3" -> updateStudent();
case "4" -> queryStudent();
case "5" -> {
System.out.println("退出");
break loop;
//System.exit(0);//停止虚拟机运行
}
default -> System.out.println("没有这个选项");
}
}
}
public static void addStudent(){
System.out.println("添加学生");
}
public static void deleteStudent(){
System.out.println("删除学生");
}
public static void updateStudent(){
System.out.println("修改学生");
}
public static void queryStudent(){
System.out.println("查询学生");
}
}
添加功能
public static void addStudent(ArrayList<Student> list){
System.out.println("添加学生");
Scanner sc = new Scanner(System.in);
Student stu = new Student();
String ID = null;
while (true) {
System.out.println("请输入学生ID");
ID = sc.next();
boolean flag = contains(list,ID);
if(flag){
System.out.println("ID已经存在,请重新录入");
}else {
stu.setId(ID);
break;
}
}
System.out.println("请输入学生姓名");
String name = sc.next();
stu.setName(name);
System.out.println("请输入学生年龄");
int age = sc.nextInt();
stu.setAge(age);
System.out.println("请输入学生家庭住址");
String address = sc.next();
stu.setAddress(address);
list.add(stu);
System.out.println("学生信息添加成功");
}
public static boolean contains(ArrayList<Student> list,String id){
for (int i = 0; i < list.size(); i++) {
if(id==list.get(i).getId()){
return true;
}
}
return false;
}
查询功能
public static void queryStudent(ArrayList<Student> list){
if(list.size()==0){
System.out.println("没有学生信息");
}
System.out.println("id\t\t姓名\t年龄\t家庭住址");
for (int i = 0; i < list.size(); i++) {
Student stu = list.get(i);
System.out.println(stu.getId()+"\t\t"+stu.getName()+"\t"+stu.getAge()+"\t"+stu.getAddress());
}
}
删除
public static void deleteStudent(ArrayList<Student> list){
System.out.println("删除学生");
Scanner sc = new Scanner(System.in);
System.out.println("输入你要删除的学生学号");
String id = sc.next();
if (getIndex(list,id)>=0){
list.remove(getIndex(list,id));
System.out.println("id为"+id+"的学生删除成功");
}else {
System.out.println("所要删除的学号不存在");
}
}
public static int getIndex(ArrayList<Student> list,String id){
for (int i = 0; i < list.size(); i++) {
if(id.equals(list.get(i).getId())){
return i;
}
}
return -1;
}
修改
public static void updateStudent(ArrayList<Student> list){
System.out.println("修改学生");
Scanner sc = new Scanner(System.in);
System.out.println("请输入你要修改学生的学号");
String id = sc.next();
int index = getIndex(list,id);
if(index == -1){
System.out.println("你所输入的学生学号不存在");
return;
}
Student stu = list.get(getIndex(list,id));
System.out.println("输入要修改的姓名");
String newName = sc.next();
stu.setName(newName);
System.out.println("输入你要修改的年龄");
int newAge = sc.nextInt();
stu.setAge(newAge);
System.out.println("输入你要修改的家庭地址");
String newAddress = sc.next();
stu.setAddress(newAddress);
System.out.println("修改完成");
}