ArrayList中字符串的过滤
发布网友
发布时间:2022-04-20 18:57
我来回答
共2个回答
热心网友
时间:2023-04-25 01:19
import java.util.*;
public class Doc {
public static void main(String[] args) {
test();
}
public static void test() {
int[] num = new int[] { 1, 4, 5, 7, 8, 9 };
List<User> list = getListUser();
List<User> newlist = new ArrayList<User>();
// 双层循环将数据里的元素和集合中的元素逐个取出,用于对比
for (int i = 0; i < num.length; i++) {
for (int j = 0; j < list.size(); j++) {
// 当数组中的元素等于user的id时将对应的name改值,并存入新的集合中
if (num[i] == list.get(j).getId()) {
list.get(j).setName(list.get(j).getName() + "pipei");
newlist.add(list.get(j));
}
}
}
for (int i = 0; i < newlist.size(); i++) {
System.out.println(newlist.get(i).getId() + "\t"
+ newlist.get(i).getName());
}
}
public static List<User> getListUser() {
List<User> list = new ArrayList<User>();
User user1 = new User();
user1.setId(1);
user1.setName("abc");
list.add(user1);
User user2 = new User();
user2.setId(2);
user2.setName("123");
list.add(user2);
User user3 = new User();
user3.setId(3);
user3.setName("asd");
list.add(user3);
User user4 = new User();
user4.setId(4);
user4.setName("qwe");
list.add(user4);
User user5 = new User();
user5.setId(5);
user5.setName("fds");
list.add(user5);
User user6 = new User();
user6.setId(6);
user6.setName("';.");
list.add(user6);
User user7 = new User();
user7.setId(7);
user7.setName("```");
list.add(user7);
User user8 = new User();
user8.setId(8);
user8.setName("!@#");
list.add(user8);
User user9 = new User();
user9.setId(9);
user9.setName("~~~");
list.add(user9);
User user10 = new User();
user10.setId(10);
user10.setName("777");
list.add(user10);
return list;
}
}
class User {
private int id;
private String name;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
热心网友
时间:2023-04-25 01:19
package com..demo;
import java.util.ArrayList;
import java.util.List;
import com..demo.bean.Student;
public class Demo {
public static void main(String[] args) {
List<String> sList = new ArrayList<String>();
List<Student> rList = new ArrayList<Student>();
List<Student> newList = new ArrayList<Student>();
Student rstu = null;
//添加10个学生
for(int i=0;i<10;i++){
rstu = new Student();
rstu.setId(i+"");
rstu.setName("张三"+i);
rList.add(rstu);
}
//添加10个ID
for(int j=9;j<19;j++){
sList.add(j+"");
}
//检索10个学生的ID
for(int m=0;m<rList.size();m++){
String rId = rList.get(m).getId()+"";
String rName = rList.get(m).getName();
//检索sList中的ID
for(int n=0;n<sList.size();n++){
String bId = sList.get(n);
//如果学生的ID与sList数组中的相同
if(rId.equals(bId)){
rstu.setId(bId);
rstu.setName(rName+"(匹配)");
//改好的名字和ID存入新的集合中
newList.add(rstu);
}
}
}
//取出新集合中学生(其实就一个相同的)
for(int q=0;q<newList.size();q++){
Student stu= newList.get(q);
System.out.println(stu.getId());
System.out.println(stu.getName());
}
}
}
package com..demo.bean;
public class Student {
private String id;
private String name;
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;
}
}