赞
踩
1、add方法向集合中添加元素;
2、get方法获取集合中的元素。
- public class ArrayListDemo1 {
- public static void main(String[] args) {
- ArrayList<String> list = new ArrayList<>();
- list.add("aaa");
- list.add("bbb");
- list.add("ccc");
- 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) + ",");
- }
- }
- }
- }
![](https://csdnimg.cn/release/blogv2/dist/pc/img/newCodeMoreWhite.png)
对于整数要使用其包装类Integer:
- public class ArrayListDemo2 {
- public static void main(String[] args) {
- ArrayList<Integer> list = new ArrayList<>();
- list.add(1);
- list.add(2);
- list.add(3);
- list.add(4);
- 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) + ",");
- }
- }
- }
- }
![](https://csdnimg.cn/release/blogv2/dist/pc/img/newCodeMoreWhite.png)
需求:定义一个集合,添加一些学生对象,并进行遍历。
学生类的属性为:姓名,年龄。
- public class ArrayListDemo3 {
- public static void main(String[] args) {
- ArrayList<Student> list = new ArrayList<>();
- Student s1 = new Student("zhang", 22);
- Student s2 = new Student("liu", 23);
- list.add(s1);
- list.add(s2);
- System.out.println(list);
- }
- }
需求:定义一个集合,添加一些学生对象,要求学生的属性键盘录入,并进行遍历。
学生类的属性为:姓名,年龄。
- public class ArrayListDemo4 {
- public static void main(String[] args) {
- ArrayList<Student> list = new ArrayList<>();
- Scanner sc = new Scanner(System.in);
- for (int i = 0; i < 2; i++) {
- Student s = new Student();
- String name = sc.next();
- int age = sc.nextInt();
- s.setName(name);
- s.setAge(age);
- list.add(s);
- }
- for (int i = 0; i < list.size(); i++) {
- System.out.println(list.get(i));
- }
- }
- }
![](https://csdnimg.cn/release/blogv2/dist/pc/img/newCodeMoreWhite.png)
需求:
1、main方法中定义一个集合,存入三个用户对象。用户属性为:id,username,password。
要求:定义一个方法,根据id查找对应的用户信息。如果存在,返回true;如果不存在,返回false 。
- public class ArrayListDemo5 {
- public static void main(String[] args) {
- ArrayList<User> list = new ArrayList<>();
- User user1 = new User("heima001", "zhang", "12345");
- User user2 = new User("heima002", "liu", "12345");
- list.add(user1);
- list.add(user2);
- String id = "heima003";
- System.out.println(selectID(list,id));
- }
- public static boolean selectID(ArrayList<User> list,String id){
- boolean flag = false;
- for (int i = 0; i < list.size(); i++) {
- if(id.equals(list.get(i))){
- flag = true;
- break;
- }
- }
- return flag;
- }
- }
![](https://csdnimg.cn/release/blogv2/dist/pc/img/newCodeMoreWhite.png)
要求:定义一个方法,根据id查找对应的用户信息。如果存在,返回索引;如果不存在,返回-1。
- public static int getIndex(ArrayList<User> list,String id){
- boolean flag = false;
- for (int i = 0; i < list.size(); i++) {
- if(id.equals(list.get(i))){
- return i;
- }
- }
- return -1;
- }
如果既要返回索引又要返回true或者false,可以将两个方法的代码合并一部分,在contains方法里调用getIndex方法,对返回值进行判断,如果大于0,则返回true,否则返回false。 即:return getIndex(list,id) >= 0;
如果一个函数要返回两个不同类型的参数,只能是两个方法分别进行返回,后面应该有新的东西可以用。
- public static boolean selectID(ArrayList<User> list,String id){
- return getIndex(list,id) >= 0;
- }
- public static int getIndex(ArrayList<User> list,String id){
- boolean flag = false;
- for (int i = 0; i < list.size(); i++) {
- if(id.equals(list.get(i))){
- return i;
- }
- }
- return -1;
- }
需求:
定义javabean类:Phone
Phone属性:品牌,价格。
main方法中定义一个集合,存入三个手机对象。分别为:小米,1000。苹果,8000。锤子,2999。
定义一个方法,将价格低于3000的手机信息返回。
- public class ArrayListDemo6 {
- public static void main(String[] args) {
- ArrayList<Phone> list = new ArrayList<>();
- Phone p1 = new Phone("小米", 1000);
- Phone p2 = new Phone("苹果", 8000);
- Phone p3 = new Phone("锤子", 2999);
- list.add(p1);
- list.add(p2);
- list.add(p3);
- getPhone(list);
- }
-
- public static void getPhone(ArrayList<Phone> list) {
- for (int i = 0; i < list.size(); i++) {
- Phone phone = list.get(i);
- if (phone.getPrice() < 3000) {
- System.out.println("手机品牌为:" + phone.getBrand() + "手机价格为:" + phone.getPrice());
- }
- }
- }
- }
![](https://csdnimg.cn/release/blogv2/dist/pc/img/newCodeMoreWhite.png)
如果不是直接输出,而是返回多个同类型的数据,可以用集合和数组进行接收封装起来。
- public class ArrayListDemo6 {
- public static void main(String[] args) {
- ArrayList<Phone> list = new ArrayList<>();
- Phone p1 = new Phone("小米", 1000);
- Phone p2 = new Phone("苹果", 8000);
- Phone p3 = new Phone("锤子", 2999);
- list.add(p1);
- list.add(p2);
- list.add(p3);
- ArrayList<Phone> list1 = getPhone(list);
- for (int i = 0; i < list1.size(); i++) {
- Phone phone = list1.get(i);
- System.out.println("手机品牌为:" + phone.getBrand() + "手机价格为:" + phone.getPrice());
-
- }
- }
-
- public static ArrayList<Phone> getPhone(ArrayList<Phone> list) {
- ArrayList<Phone> list1 = new ArrayList<>();
- for (int i = 0; i < list.size(); i++) {
- Phone phone = list.get(i);
- if (phone.getPrice() < 3000) {
- list1.add(phone);
- }
- }
- return list1;
- }
- }
![](https://csdnimg.cn/release/blogv2/dist/pc/img/newCodeMoreWhite.png)
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。