Java arraylist中的最小2个值

Java arraylist中的最小2个值,java,arraylist,Java,Arraylist,对于我的计算机科学课程的最后一个项目,我需要制作一个项目,该项目有两个类,读取带有矩形坐标(x,y,宽度,高度)的CSV,将它们放在数组列表中,并打印出两个与它们的角(x,y)距离最小的矩形我已经设法打印出最小的矩形,但我现在不知道如何打印第二个最小的矩形 这是我的密码 public class Week09 { public static void main(String[] args)throws IOException { String theFile;

对于我的计算机科学课程的最后一个项目,我需要制作一个项目,该项目有两个类,读取带有矩形坐标(x,y,宽度,高度)的CSV,将它们放在数组列表中,并打印出两个与它们的角(x,y)距离最小的矩形我已经设法打印出最小的矩形,但我现在不知道如何打印第二个最小的矩形

这是我的密码

    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");
}

}

因此,本质上,您需要浏览列表,找到仍然是
的最小距离,或者,在计算距离的相同
循环中,保存2个距离,一个最小,一个最小的正方形(在将值设置为最小之前)

困惑,我知道

如下所示:假设您有一个5个值的列表5、4、3、2和1

要得到最小的,你只需在列表中浏览一次,对吗。对于你做的每一个数字

if (min > currentNumber) then min=currentNumber,
现在假设您有两个变量:
min1和min2

if (min1 > currentNumber) then {
  min2 = min1;
  min1 = currentNumber;
}
最后,min1将是“1”,min2将是“2”。

Rectangle firstRectangle=null,secondRectangle=null;
双下距离=-1.0;
对于(int index1=0;index1
执行此代码后,原点最近的两个矩形将位于
firstRectangle
secondRectangle


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

好的,我不确定你到底在问什么,但下面是找到矩形中最近的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);
    }
ArrayList rects=new ArrayList();
double minDistance=double.MAX_值;
矩形r1;
矩形r2;
用于(矩形rect1:rects){
用于(矩形rect2:rects){
如果(!rect1.等于(rect2)){

如果(距离(rect1.x,rect1.y,rect2.x,rect2.y)

package-com;
导入java.util.ArrayList;
导入java.util.Collections;
导入java.util.List;
公共班机{
/**
*@param args
*/
公共静态void main(字符串[]args){
List collRectangles=new ArrayList();
//请检查坐标(如果此表格坐标为随机值
添加(新矩形(新点(0,0)、新点(0,1)、新点(1,1)、新点(1,0));
添加(新矩形(新点(2,2)、新点(2,4)、新点(6,4)、新点(6,2));
添加(新矩形(新点(3,3)、新点(3,5)、新点(8,5)、新点(8,3));
Collections.sort(coll矩形);
矩形smallestRectangle=collRectangles.get(collRectangles.size()-1);
Rectangle secondSmallestRectangle=collRectangles.get(collRectangles.size()-2);
}
}
类矩形实现可比较的{
私人点a;
专用点B;
专用点c;
专用点D;
公共矩形(){
}
公共矩形(点A、点B、点C、点D){
超级();
this.pointA=pointA;
this.pointB=pointB;
this.pointC=pointC;
this.pointD=pointD;
}
公共点getPointA(){
返回点A;
}
公共无效设定点A(点A){
this.pointA=pointA;
}
公共点getPointB(){
返回点B;
}
公共无效设置点B(点B){
this.pointB=pointB;
}
公共点getPointC(){
返回点c;
}
公共无效设置点C(点C){
this.pointC=pointC;
}
公共点getPointD(){
返回点D;
}
公共无效设置点D(点D){
this.pointD=pointD;
}
公共整数比较(矩形矩形){
//比较对角线
return(int)Math.round(Utility.finddInstanceBetweenPoints(this.pointA,this.pointC)
-点之间的Utility.finddInstance(rect.pointA,rect.pointC));
}
}
类点{
公共双协调;
公共双协调;
公共点(双坐标,双坐标){
超级();
this.coordinateX=coordinateX;
this.coordinateY=coordinateY;
}
公共双getCoordinateX(){
返回坐标;
}
公共无效设置坐标(双坐标){
this.coordinateX=coordinateX;
}
公共双getCoordinateY(){
回归协调;
}
公共无效设置坐标(双坐标){
this.coordinateY=coordinateY;
}
}
类效用{
点之间的公共静态双FindInstance(点p1、点p2){
double yCoordinateDifference=p1.getCoordinateY()-p2.getCoordinateY();
double XCoordinationDifference=p1.getCoordinateX()-p2.getCoordinateX();
返回Math.sqrt((yCoordinatedDifference*yCoordinatedDifference)+(xCoordinatedDifference*xCoordinatedDifference));
}
}

是否可以使/实现您的矩形类具有可比性,并使用自动比较和排序对象的树集?请看一看以澄清,您希望从数组中打印两个矩形,其原点(x,y)彼此最接近?或任何角点(x,y)的原点最接近?(x,y)是左上角的坐标,它是CSV中的x和y(x,y,宽度,高度)。与它们的角(x,y)的最小距离到底意味着什么?
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);
    }
}
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);
        }
    }
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));
    }
}