当前位置:   article > 正文

ArrayList练习

ArrayList练习

练习1:添加字符串和整数并进行遍历

1、add方法向集合中添加元素;

2、get方法获取集合中的元素。

  1. public class ArrayListDemo1 {
  2. public static void main(String[] args) {
  3. ArrayList<String> list = new ArrayList<>();
  4. list.add("aaa");
  5. list.add("bbb");
  6. list.add("ccc");
  7. System.out.print("[");
  8. for (int i = 0; i < list.size(); i++) {
  9. if(i == list.size() - 1){
  10. System.out.print(list.get(i) + "]");
  11. } else {
  12. System.out.print(list.get(i) + ",");
  13. }
  14. }
  15. }
  16. }

对于整数要使用其包装类Integer: 

  1. public class ArrayListDemo2 {
  2. public static void main(String[] args) {
  3. ArrayList<Integer> list = new ArrayList<>();
  4. list.add(1);
  5. list.add(2);
  6. list.add(3);
  7. list.add(4);
  8. System.out.print("[");
  9. for (int i = 0; i < list.size(); i++) {
  10. if(i == list.size() - 1){
  11. System.out.print(list.get(i) + "]");
  12. } else {
  13. System.out.print(list.get(i) + ",");
  14. }
  15. }
  16. }
  17. }

练习2:添加学生对象并进行遍历 

需求:定义一个集合,添加一些学生对象,并进行遍历。

学生类的属性为:姓名,年龄。

  1. public class ArrayListDemo3 {
  2. public static void main(String[] args) {
  3. ArrayList<Student> list = new ArrayList<>();
  4. Student s1 = new Student("zhang", 22);
  5. Student s2 = new Student("liu", 23);
  6. list.add(s1);
  7. list.add(s2);
  8. System.out.println(list);
  9. }
  10. }

需求:定义一个集合,添加一些学生对象,要求学生的属性键盘录入,并进行遍历。

学生类的属性为:姓名,年龄。

  1. public class ArrayListDemo4 {
  2. public static void main(String[] args) {
  3. ArrayList<Student> list = new ArrayList<>();
  4. Scanner sc = new Scanner(System.in);
  5. for (int i = 0; i < 2; i++) {
  6. Student s = new Student();
  7. String name = sc.next();
  8. int age = sc.nextInt();
  9. s.setName(name);
  10. s.setAge(age);
  11. list.add(s);
  12. }
  13. for (int i = 0; i < list.size(); i++) {
  14. System.out.println(list.get(i));
  15. }
  16. }
  17. }

练习3:添加用户并判断是否存在

需求:
1、main方法中定义一个集合,存入三个用户对象。用户属性为:id,username,password。

要求:定义一个方法,根据id查找对应的用户信息。如果存在,返回true;如果不存在,返回false 。

  1. public class ArrayListDemo5 {
  2. public static void main(String[] args) {
  3. ArrayList<User> list = new ArrayList<>();
  4. User user1 = new User("heima001", "zhang", "12345");
  5. User user2 = new User("heima002", "liu", "12345");
  6. list.add(user1);
  7. list.add(user2);
  8. String id = "heima003";
  9. System.out.println(selectID(list,id));
  10. }
  11. public static boolean selectID(ArrayList<User> list,String id){
  12. boolean flag = false;
  13. for (int i = 0; i < list.size(); i++) {
  14. if(id.equals(list.get(i))){
  15. flag = true;
  16. break;
  17. }
  18. }
  19. return flag;
  20. }
  21. }

要求:定义一个方法,根据id查找对应的用户信息。如果存在,返回索引;如果不存在,返回-1。 

  1. public static int getIndex(ArrayList<User> list,String id){
  2. boolean flag = false;
  3. for (int i = 0; i < list.size(); i++) {
  4. if(id.equals(list.get(i))){
  5. return i;
  6. }
  7. }
  8. return -1;
  9. }

如果既要返回索引又要返回true或者false,可以将两个方法的代码合并一部分,在contains方法里调用getIndex方法,对返回值进行判断,如果大于0,则返回true,否则返回false。 即:return getIndex(list,id) >= 0;

如果一个函数要返回两个不同类型的参数,只能是两个方法分别进行返回,后面应该有新的东西可以用。

  1. public static boolean selectID(ArrayList<User> list,String id){
  2. return getIndex(list,id) >= 0;
  3. }
  4. public static int getIndex(ArrayList<User> list,String id){
  5. boolean flag = false;
  6. for (int i = 0; i < list.size(); i++) {
  7. if(id.equals(list.get(i))){
  8. return i;
  9. }
  10. }
  11. return -1;
  12. }

练习4:添加手机对象并返回要求的数据 

需求:
定义javabean类:Phone

Phone属性:品牌,价格。

main方法中定义一个集合,存入三个手机对象。分别为:小米,1000。苹果,8000。锤子,2999。

定义一个方法,将价格低于3000的手机信息返回。

  1. public class ArrayListDemo6 {
  2. public static void main(String[] args) {
  3. ArrayList<Phone> list = new ArrayList<>();
  4. Phone p1 = new Phone("小米", 1000);
  5. Phone p2 = new Phone("苹果", 8000);
  6. Phone p3 = new Phone("锤子", 2999);
  7. list.add(p1);
  8. list.add(p2);
  9. list.add(p3);
  10. getPhone(list);
  11. }
  12. public static void getPhone(ArrayList<Phone> list) {
  13. for (int i = 0; i < list.size(); i++) {
  14. Phone phone = list.get(i);
  15. if (phone.getPrice() < 3000) {
  16. System.out.println("手机品牌为:" + phone.getBrand() + "手机价格为:" + phone.getPrice());
  17. }
  18. }
  19. }
  20. }

 如果不是直接输出,而是返回多个同类型的数据,可以用集合和数组进行接收封装起来

  1. public class ArrayListDemo6 {
  2. public static void main(String[] args) {
  3. ArrayList<Phone> list = new ArrayList<>();
  4. Phone p1 = new Phone("小米", 1000);
  5. Phone p2 = new Phone("苹果", 8000);
  6. Phone p3 = new Phone("锤子", 2999);
  7. list.add(p1);
  8. list.add(p2);
  9. list.add(p3);
  10. ArrayList<Phone> list1 = getPhone(list);
  11. for (int i = 0; i < list1.size(); i++) {
  12. Phone phone = list1.get(i);
  13. System.out.println("手机品牌为:" + phone.getBrand() + "手机价格为:" + phone.getPrice());
  14. }
  15. }
  16. public static ArrayList<Phone> getPhone(ArrayList<Phone> list) {
  17. ArrayList<Phone> list1 = new ArrayList<>();
  18. for (int i = 0; i < list.size(); i++) {
  19. Phone phone = list.get(i);
  20. if (phone.getPrice() < 3000) {
  21. list1.add(phone);
  22. }
  23. }
  24. return list1;
  25. }
  26. }
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/代码探险家/article/detail/790952
推荐阅读
相关标签
  

闽ICP备14008679号