二维数组问题

时间:2013-12-13 08:11:13

标签: java arrays

好的,所以我在课堂上有一个赋值,用于编写一个返回2D数组中最大元素位置的方法。返回值是一个1D数组,其中包含两个元素,指示2D数组中最大元素的行和列。

public static void main(String[] args) {
    int[] loc=new int[2];
    Scanner scan=new Scanner(System.in);
    int x,y;
    System.out.print("Enter the size of your two-dimensional array. Length then height: ");
    x=scan.nextInt();
    y=scan.nextInt();
    double [][]matrix=new double[x][y];
    System.out.println("Enter your two-dimensional array row by column: ");
    for (int i=0;i<y;i++){
        for (int j=0;j<x;j++){
            matrix[j][i]=scan.nextDouble();
            System.out.print("i: "+i+" j: "+j + " ");
        }
        System.out.println();
    }
    System.out.println();
    for (int i = 0; i<x; i++){
        for (int j = 0; j<y; j++){

            System.out.print(matrix[i][j] + ("i: "+i+" j: "+j + " "));

        }
         System.out.println();
    }
    loc=locateLargest(matrix,x,y);
        System.out.println(matrix[loc[0]][loc[1]]);


}
public static int[] locateLargest(double[][] a ,int x, int y){
    int m=0,n=0;
    int[] location={0,0};
    double largest=a[0][0];
    for (int i=0;i<x;i++){
        for (int j=0;j<y;j++){
            System.out.println("X: "+i+" Y: "+j +" value: " + a[i][j]);
            if (a[i][j]>largest){

                location[0]=i;
                m=i;
                location[1]=j;
                n=i;
            }
        }
    }
    System.out.println("the location of the largest element of you two-dimensional array is : ("  + location[0] + ", " + location[1] + ")");
    System.out.println(a[m][n]);
    return location;
}

这是我的代码。它非常草率,可能没有多大意义。我包括main(String [] args)和我的方法,因为两者都需要设置它。当我运行它时,我得到了错误的答案,我正在尝试通过打印出值来解决问题(我已将这些打印行留在我的代码中),我得到的答案是错误的。我相信它与我的数组元素的输入方式有关。

我正在使用的阵列是23.5 35 2 10 4.5 3 45 3.5 35 44 5.5 9.6并且我得到的答案是(2,2)如果我正确读取它是5.5。 X的值为3,Y为4.

有关为什么我的代码返回错误答案的任何帮助将不胜感激。谢谢。

编辑:我添加了X和Y的值。

3 个答案:

答案 0 :(得分:0)

在块中找到更大的值后,您需要重新分配变量largest

        if (a[i][j]>largest){
            largest=a[i][j]; //current value is largest
            location[0]=i;
            location[1]=j;
        }

删除变量mn,它们是多余的。

答案 1 :(得分:0)

我认为你使用循环的方式是正确的找到最大的数字,唯一的问题是重新分配最大值没有在locateLargest函数中完成,

public static int[] locateLargest(double[][] a ,int x, int y)
{
    int m=0,n=0;
    int[] location={0,0};
    double largest=a[0][0];
    for (int i=0;i<x;i++)
    {
        for (int j=0;j<y;j++)
        {
            System.out.println("X: "+i+" Y: "+j +" value: " + a[i][j]);
            if (a[i][j]>largest)
            {
                location[0]=i;
                m=i; //Why you need this extra variables 
                location[1]=j;
                n=i; //Why you need this extra variables 
                largest = a[i][j];
            }
        }
    }
    System.out.println("the location of the largest element of you two-dimensional array is : ("  + location[0] + ", " + location[1] + ")");
    System.out.println(a[m][n]);
    return location;
}

答案 2 :(得分:0)

这个问题已得到解答,但我认为你也可以考虑如何在不必询问Stack Overflow的情况下解决这类问题。即使你的任务没有要求,我强烈建议你开始做测试驱动开发(TDD) - http://www.javaworld.com/article/2073090/testing-debugging/getting-started-with-test-driven-development.html

如果没有,至少尝试并“减少你的反馈循环”。例如,以下代码块允许您轻松(并可视)构建可以使用的测试数组(而不是每次要测试代码时都必须接受用户输入)。

这将允许您在代码中构建5个略有不同的数组,并查看哪些数组失败,这可以帮助您找出原因。

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class LargestNumber {

    public static void main(String[] args){
        TableBuilder<Integer> tableBuilder = new TableBuilder<Integer>()
                .row(1,   2, 3)
                .row(3,   2, 1)
                .row(3, 100, 1);
        print(tableBuilder.build());
    }


    static class TableBuilder<T>{
        List<List<T>> table = new ArrayList<List<T>>();

        TableBuilder<T> row(T... rowData){
            table.add(Arrays.asList(rowData));
            return this;
        }

        T[][] build(){
            T[][] tableAsArray = (T[][]) new Object[table.size()][];
            int arrayIndex = 0;
            for(List<T> row: table){
                tableAsArray[arrayIndex++] = (T[])row.toArray() ;
            }
            return tableAsArray;
        }
    }

    static void print(Object[][] table){
        for(Object[] row:table){
            for(Object cell:row){
                System.out.print(String.format("%1$" + 5 + "s", cell));
            }
            System.out.println();
        }

    }
}
相关问题