ArrayListTest

package java_learning;
import java.util.*;

public class ArrayListTest {
	public static void main(String[]args){
		//ArrayList<String> ah = new ArrayList<String>();
		ArrayList al = new ArrayList();
		//al.add("winsun");
		//al.add("wangry");
		//al.add("xiangming");
		 
		al.add(new Point(1,1));
		al.add(new Point(2,2));
		al.add(new Point(3,3));
		al.add(new Point(4,4));
		
		for(int i=0;i<al.size();i++){
			System.out.println(al.get(i));
		}
			
		System.out.println(al);

		Object[] objs = al.toArray();
		System.out.println("objs"+ objs);
		for(int j=0; j<al.size(); j++){
			System.out.println("objs[" + j + "] 为: "+ objs[j]);
		} 
	}
}

class Point{
	int x;
	int y;
	Point(int x, int y){
		 this.x = x;
		 this.y = y;
	}
	public String toString(){
		return "x="+ x + ",y=" + y;
	}
	
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
这段代码有以下问题: 1. 没有将三个手机对象添加到集合中。 2. 在调用`choice`方法时,传入的集合为空,应该先将三个手机对象添加到集合中再调用方法。 3. 在`choice`方法中,只判断了价格等于3000的情况,而没有判断价格小于3000的情况。应该将`if`语句中的`==`改为`<`。 4. 在`choice`方法中,当找到第一个价格小于3000的手机时,直接返回该手机在集合中的索引,而没有将该手机信息返回。应该将返回值类型改为`Phone`类,并返回该手机对象。 以下是修改后的代码: ```java package ArrayListTest; import java.util.ArrayList; public class ArrayListTest6 { 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); Phone phone = choice(list); if (phone != null) { System.out.println("价格低于3000的手机信息为:" + phone.getBrand() + ", " + phone.getPrice()); } else { System.out.println("没有价格低于3000的手机。"); } } public static Phone choice(ArrayList<Phone> list) { for (int i = 0; i < list.size(); i++) { Phone p = list.get(i); int pp = p.getPrice(); if (pp < 3000) { return p; } } return null; } static class Phone { private String brand; private int price; public Phone(String brand, int price) { this.brand = brand; this.price = price; } public String getBrand() { return brand; } public int getPrice() { return price; } } } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值