在java中,定义一个list,向里面添加几个数字,怎样实现选择性的输出,比如,输出其最大值,或者最小值。
展开全部
private double x; //私有变量 双精度型 的 x
private double y; //私有变量 双精度型 的 y
Point(double x1, double y1) { //点类的带两个参数的构造方法,
x = x1; //把参数x1的值赋给x
y = y1; //把常识y1的值赋给y
}
public double getX()
public double getY()
public void setX(double i)
public void setY(double i)
}
class Circle { //圆类
private Point o;
private double radius;
Circle(Point p, double r) {
o = p;
radius = r;
}
Circle(double r) {
o = new Point(0.0, 0.0);
radius = r;
}
boolean contains(Point p) {
double x = p.getX() - o.getX();
double y = p.getY() - o.getY();
if(x*x + y*y > radius * radius) return false;
else return true;
}
public void setO(double x, double y) {
o.setX(x);
o.setY(y);
}
public Point getO()
public double getRadius()
public void setRadius(double r)
public double area() {
return 3.14 * radius * radius;
}
}
public class TestCircle { //主类
public static void main(String args[]) {
Circle c1 = new Circle(new Point(1.0,2.0), 2.0);
Circle c2 = new Circle(5.0);
System.out.println("c1:("+c1.getO().getX()+"," +c1.getO().getY()+"),"+c1.getRadius());
System.out.println("c2:("+c2.getO().getX()+","+c2.getO().getY()+"),"+c2.getRadius());
System.out.println("c1 area = "+c1.area());
System.out.println("c1 area = "+c2.area());
c1.setO(5,6);
c2.setRadius(9.0);
System.out.println("c1:("+c1.getO().getX()+","+c1.getO().getY()+"),"+c1.getRadius());
System.out.println("c2:("+c2.getO().getX()+","+c2.getO().getY()+"),"+c2.getRadius());
System.out.println("c1 area = "+c1.area());
System.out.println("c1 area = "+c2.area());
Point p1 = new Point(5.2, 6.3);
System.out.println(c1.contains(p1));
System.out.println(c1.contains(new Point(10.0,9.0)));
}
}
现成的,不过没给你改。自己按自己要求改下。
private double y; //私有变量 双精度型 的 y
Point(double x1, double y1) { //点类的带两个参数的构造方法,
x = x1; //把参数x1的值赋给x
y = y1; //把常识y1的值赋给y
}
public double getX()
public double getY()
public void setX(double i)
public void setY(double i)
}
class Circle { //圆类
private Point o;
private double radius;
Circle(Point p, double r) {
o = p;
radius = r;
}
Circle(double r) {
o = new Point(0.0, 0.0);
radius = r;
}
boolean contains(Point p) {
double x = p.getX() - o.getX();
double y = p.getY() - o.getY();
if(x*x + y*y > radius * radius) return false;
else return true;
}
public void setO(double x, double y) {
o.setX(x);
o.setY(y);
}
public Point getO()
public double getRadius()
public void setRadius(double r)
public double area() {
return 3.14 * radius * radius;
}
}
public class TestCircle { //主类
public static void main(String args[]) {
Circle c1 = new Circle(new Point(1.0,2.0), 2.0);
Circle c2 = new Circle(5.0);
System.out.println("c1:("+c1.getO().getX()+"," +c1.getO().getY()+"),"+c1.getRadius());
System.out.println("c2:("+c2.getO().getX()+","+c2.getO().getY()+"),"+c2.getRadius());
System.out.println("c1 area = "+c1.area());
System.out.println("c1 area = "+c2.area());
c1.setO(5,6);
c2.setRadius(9.0);
System.out.println("c1:("+c1.getO().getX()+","+c1.getO().getY()+"),"+c1.getRadius());
System.out.println("c2:("+c2.getO().getX()+","+c2.getO().getY()+"),"+c2.getRadius());
System.out.println("c1 area = "+c1.area());
System.out.println("c1 area = "+c2.area());
Point p1 = new Point(5.2, 6.3);
System.out.println(c1.contains(p1));
System.out.println(c1.contains(new Point(10.0,9.0)));
}
}
现成的,不过没给你改。自己按自己要求改下。
展开全部
ArrayList<Integer> items = new ArrayList<Integer>();
items.add(1);
items.add(3);
items.add(2);
//然后取最大值:
int a = 0;
for(int i=0;i<items.size();i++)
{
if(i==0)
{
a = items.get(i);
}
else
{
a = Math.max(a,items.get(i));
}
}
if(items.size() > 0)
{
System.out.println("最大值:" + a);
}
else
{
System.out.println("当前list为空");
}
items.add(1);
items.add(3);
items.add(2);
//然后取最大值:
int a = 0;
for(int i=0;i<items.size();i++)
{
if(i==0)
{
a = items.get(i);
}
else
{
a = Math.max(a,items.get(i));
}
}
if(items.size() > 0)
{
System.out.println("最大值:" + a);
}
else
{
System.out.println("当前list为空");
}
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
不知道你想要的是什么
定义个List<Integer> 然后追加数字
然后用Collections.sort(list, new Comparator<Integer>() 给拍个序 升序降序随便
然后相输出最大、最小 自己判断下就行了
定义个List<Integer> 然后追加数字
然后用Collections.sort(list, new Comparator<Integer>() 给拍个序 升序降序随便
然后相输出最大、最小 自己判断下就行了
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
不能直接的实现选择性输出,只能靠自己代码实现··
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询