来自arraylist的最小2个值

时间:2014-06-12 02:43:58

标签: java arraylist

对于我的计算机类科学课中的最终项目,我需要创建一个包含2个类的项目并读取带有矩形坐标的CSV(x,y,width,height)将它们放在数组列表中并打印与角(x,y)距离最小的2个矩形我已经设法打印出最小的矩形,但我现在不知道如何打印第二个最小的矩形。

这是我的代码

    public class Week09 {
    public static void main(String[] args)throws IOException {
        String theFile;
        theFile = getTheFileName();
        ArrayList<Rectangle> arrayRectangle;
        arrayRectangle = getArraylist(theFile);
        displaySameArea(arrayRectangle,"Rectangles with same area: ");
        displaysmallDist(arrayRectangle,"Recangles with smallest distance: ");
    }
    public static String getTheFileName(){
        String theFile;
        JFileChooser jfc = new JFileChooser();
        jfc.showOpenDialog(null);
        return theFile = jfc.getSelectedFile().getAbsolutePath();
    }
    public static ArrayList<Rectangle> getArraylist(String s) throws IOException {
        ArrayList <Rectangle> arrayRectangle = new ArrayList <Rectangle>(); 
        FileReader fr = new FileReader(s);
        BufferedReader br = new BufferedReader(fr);
        int x = 0;
        int y = 0;
        int width = 0;
        int height = 0;
        String aLine;
        String arrayLine = "[,]";
        try{
        while ( (aLine=br.readLine()) != null){
            String[] a = aLine.split(arrayLine);
            x = Integer.parseInt(a[0]);
            y = Integer.parseInt(a[1]);
            width = Integer.parseInt(a[2]);
            height = Integer.parseInt(a[3]);
            Rectangle b = new Rectangle(x,y,width,height);
            arrayRectangle.add(b);
            }
        }
        catch (IOException e) {             
            e.printStackTrace();
        }
        return arrayRectangle;
    }
public static void displaysmallDist(ArrayList<Rectangle> arrayRectangle, String s) {
    int x = 0;
    int y = 0;
    int width = 0;
    int height = 0;
    int count1 = 0;
    int count2 = 1;
    Rectangle c = new Rectangle (x,y,width,height);
    Rectangle d = new Rectangle (x,y,width,height);
    double lowestDistance = Double.MAX_VALUE;

    for(int n = 0; n < arrayRectangle.size(); n++) {
        c = arrayRectangle.get(n);

        for(int j = n+1; j < arrayRectangle.size(); j++) {
            double nextDistance;
            d = arrayRectangle.get(j);
            nextDistance = c.distance(d);
            if (nextDistance < lowestDistance)
            {
                lowestDistance = nextDistance;
                count1 = n;
                count2 = j;          
            }                                                            
        }
    }
    System.out.print(s + arrayRectangle.get(count1).toString() + arrayRectangle.get(count2).toString() + "\n");
}

}

这是我的第二堂课

public class Rectangle {
private int x;
private int y;
private int width;
private int height;

public Rectangle(int x,int y,int width,int height){
    this.x = x;
    this.y = y;
    this.width = width;
    this.height = height;       
}
public Rectangle(Rectangle a)  {
    x = a.x;
    y = a.y;
    width = a.width;
    height = a.height;
}
public void setX (int x){
    this.x = x; 
}
public int getX(){
    return x;
}
public void setY (int y){
    this.y = y;
}
public int getY(){
    return y;
}
public void setWidth (int width){
    this.width = width;
}
public int getWidth(){
    return width;
}
public void setHeight (int height){
    this.height = height;
}
public int getHeight(){
    return height;
}
public double getArea(){
    return this.width * this.height;

}
public double distance(Rectangle y){
    return Math.sqrt((this.x - y.x)*(this.x - y.x) + (this.y - y.y)*(this.y - y.y));
}
public boolean equals(Rectangle a){
    if (this.width * this.height != a.width * a.height) return false;
    return true;
}
public String toString(){
    return "Rectangle corner at ("+ x +"," + y + ") Width = " + width + " Height = " + height + " ";
}

}

4 个答案:

答案 0 :(得分:0)

所以基本上你需要浏览列表,找到仍然是> lowestDistance OR的最小值,在你计算距离的同一for-loop中,保存2个距离,一个用于最小值,一个用于最小的正方形(在将值设置为最小值之前)。

令人困惑,我知道。

  

像这样:想象一下,你有一个包含5个值5,4,3,2和1的列表。

为了获得最小的一次,你可以通过列表一次,对吧。对于你做的每个号码

if (min > currentNumber) then min=currentNumber,

现在想象你有两个变量:min1 and min2

if (min1 > currentNumber) then {
  min2 = min1;
  min1 = currentNumber;
}

最后,min1将是&#34; 1&#34;和min2将是&#34; 2&#34;。

答案 1 :(得分:0)

Rectangle firstRectangle = null, secondRectangle = null;
double lowestDistance = -1.0;
for(int index1 = 0; index1 < arrayRectangle.size() - 1; index1++) {
    Rectangle r1 = arrayRectangle.get(index1);
    for(int index2 = index1 + 1; index2 < arrayRectangle.size(); index2++) {
        Rectangle r2 = arrayRectangle.get(index2);
        if(lowestDistance == -1 || r1.distance(r2) < lowestDistance) {
            firstRectangle = r1;
            secondRectangle = r2;
            lowestDistance = r1.distance(r2);
    }
}

执行此代码后,原点最近的两个矩形将位于firstRectanglesecondRectangle

编辑:因为这是循环中的循环,所以我对其进行了优化,以便每对矩形只检查一次。

答案 2 :(得分:0)

好的,我不确定你在问什么,但是这里是找到矩形中最近的2个(x,y)点的方法:

ArrayList<Rectangle> rects = new ArrayList<Rectangle>();
    double minDistance = Double.MAX_VALUE;
    Rectangle r1;
    Rectangle r2;
    for(Rectangle rect1 : rects){
        for(Rectangle rect2 : rects){
            if(!rect1.equals(rect2)){
                if(distance(rect1.x, rect1.y, rect2.x, rect2.y)<minDistance){
                    minDistance = distance(rect1.x, rect1.y, rect2.x, rect2.y);
                    r1 = rect1;
                    r2 = rect2;
                }
            }
        }
    }

    public double distance(double x1, double y1, double x2, double y2){
        return Math.pow(Math.pow(x2-x1, 2)+Math.pow(y2-y1, 2), 0.5);
    }

注意:如果你想提高效率,那就像你之前做的那样:

for(int n = 0; n < rects.size(); n++){
        Rectangle rect1 = rects.get(n);
        for(int m = n +1; m < rects.size(); m++){
            Rectangle rect2 = rects.get(m);
        }
    }

答案 3 :(得分:0)

我猜你看起来像这样。这没有经过测试。相应地改变。 &#34; PRINT&#34;矩形你的方式:)

package com;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class Main {

    /**
     * @param args
     */
    public static void main(String[] args) {
        List<Rectangle> collRectangles = new ArrayList<Rectangle>();
        //Please check co-ordinates (if this form co-ordinate these are random values
        collRectangles.add(new Rectangle(new Point(0,0), new Point(0,1), new Point(1,1), new Point(1,0)) );
        collRectangles.add(new Rectangle(new Point(2,2), new Point(2,4), new Point(6,4), new Point(6,2)) );
        collRectangles.add(new Rectangle(new Point(3,3), new Point(3,5), new Point(8,5), new Point(8,3)) );
        Collections.sort(collRectangles);
        Rectangle smallestRectangle = collRectangles.get(collRectangles.size()-1);
        Rectangle secondSmallestRectangle = collRectangles.get(collRectangles.size()-2);
    }
}

class Rectangle implements Comparable<Rectangle> {
    private Point pointA;
    private Point pointB;
    private Point pointC;
    private Point pointD;


    public Rectangle(){

    }

public Rectangle(Point pointA, Point pointB, Point pointC, Point pointD) {
        super();
        this.pointA = pointA;
        this.pointB = pointB;
        this.pointC = pointC;
        this.pointD = pointD;
    }

public Point getPointA() {
        return pointA;
    }

    public void setPointA(Point pointA) {
        this.pointA = pointA;
    }

    public Point getPointB() {
        return pointB;
    }

    public void setPointB(Point pointB) {
        this.pointB = pointB;
    }

    public Point getPointC() {
        return pointC;
    }

    public void setPointC(Point pointC) {
        this.pointC = pointC;
    }

    public Point getPointD() {
        return pointD;
    }

    public void setPointD(Point pointD) {
        this.pointD = pointD;
    }

    public int compareTo(Rectangle rect){       
        //compare diagonals
        return (int) Math.round(Utility.findDistanceBetweenPoints(this.pointA, this.pointC) 
                                            - Utility.findDistanceBetweenPoints(rect.pointA, rect.pointC));
    }



}

class Point {
    public double coordinateX;
    public double coordinateY;


    public Point(double coordinateX, double coordinateY) {
        super();
        this.coordinateX = coordinateX;
        this.coordinateY = coordinateY;
    }
    public double getCoordinateX() {
        return coordinateX;
    }
    public void setCoordinateX(double coordinateX) {
        this.coordinateX = coordinateX;
    }
    public double getCoordinateY() {
        return coordinateY;
    }
    public void setCoordinateY(double coordinateY) {
        this.coordinateY = coordinateY;
    }



}
class Utility {

    public static double findDistanceBetweenPoints(Point p1, Point p2){
        double yCoordinateDifference = p1.getCoordinateY() - p2.getCoordinateY();
        double xCoordinateDifference = p1.getCoordinateX() - p2.getCoordinateX();
        return Math.sqrt((yCoordinateDifference*yCoordinateDifference) + (xCoordinateDifference*xCoordinateDifference));
    }
}