如何在二维数组中找到最亮点

时间:2015-02-09 19:52:42

标签: java arrays 2d point light

我正在试图弄清楚如何使用这些方法来完成这个任务,但我还是比较新的Java,不知道该去哪里。我们的数组文件是:2D Array

以下是作业:Assignment

我只是在寻找有关数学的一些见解以及使用这些方法开始使用的方法。我不是要求任何人做我的作业!感谢您提前提供任何帮助!

这是我到目前为止所做的:

public static void main(String[] args) throws FileNotFoundException {

    File skyimage = new File("skyimage.txt");
    Scanner scan = new Scanner(skyimage);

    int r, c;

    r = scan.nextInt();
    c = scan.nextInt();

    int sky[][] = new int[r][c];


    for(r = 0; r < sky.length; r++){
        for(c = 0; c < sky[r].length; c++)
            sky[r][c] = scan.nextInt();
        }


    printArray(sky);
    lightSource(sky, c);


}//end main



private static void printArray(int[][] sky) {

    for(int r = 0; r < sky.length; r++){
        for(int c = 0; c < sky[r].length; c++){
            System.out.printf("%5d", sky[r][c]);
        }
        System.out.println();
    }
}


public static void lightSource(int sky[][], int n){
    Point[] lightPoint = new Point[n];

    for(int r = 0; r < sky.length; r++){
        for(int c = 0; c < sky[r].length; c++){
            new Point(r, c);
        }
    }

    System.out.println("The brightest interior point is located at: " + lightPoint);

}//end method



public static void darkSource(){

}//end method



public static void filterImage(){

}//end method



public static void negativeImage(){

}//end method

1 个答案:

答案 0 :(得分:0)

读完输入后,您只需要几种实用方法 (例如sumgetNeighboursisValidPoint)以及一些循环。

这是一个草图(不是一个完整的解决方案) 在其中,我对样本sky输入进行了硬编码。

import java.awt.Point;
import java.util.ArrayList;
import java.util.List;

public class Test045 {

    private static int[][] sky = {
        {10,2,2},
        {10,5,1},
        {1,2,2}
    };

    public static void main(String[] args) {
        Point min = null;
        Point max = null;
        Integer sumMin = null;
        Integer sumMax = null;
        Integer s = null;
        for (int i=0; i<sky.length; i++){
            for (int j=0; j<sky[0].length; j++){
                s=sum(getNeighbours(i, j));

                if (sumMin==null || sumMin > s){
                    min = new Point(i,j);
                    sumMin = s;
                }

                if (sumMax==null || sumMax < s){
                    max = new Point(i,j);
                    sumMax = s;
                }
            }
        }

        System.out.println("Max Light at: " + max.x + ", " + max.y + 
                           " ; MAX Light = " + sumMax);
        System.out.println("Min Light at: " + min.x + ", " + min.y + 
                           " ; MIN Light = " + sumMin);
    }

    private static int sum(List<Point> lst){
        int sum = 0;
        for (Point p : lst){
            sum += sky[p.x][p.y];
        }
        return sum;
    }

    private static List<Point> getNeighbours(int ip, int jp){
        List<Point> lst = new ArrayList<Point>();
        for (int i=-1; i<=1; i++){
            for (int j=-1; j<=1; j++){
                if (isValidPoint(ip+i, jp+j)){
                    lst.add(new Point(ip+i, jp+j));
                }
            }
        }
        return lst;
    }

    private static boolean isValidPoint(int i, int j){
        return 
            i >= 0 && i < sky.length &&
            j >= 0 && j < sky[0].length;
    }

}