简体   繁体   English

二维数组问题

[英]two-dimensional array issues

Okay, so I have an assignment in class to write a method that returns the location of the largest element in a 2D array. 好的,所以我在课堂上有一个作业要编写一个方法,该方法返回2D数组中最大元素的位置。 The return value is a 1D array that contains two elements that indicate the row and column of the largest element of the 2D array. 返回值是一个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;
}

This is my code. 这是我的代码。 It's pretty sloppy and may not make a whole lot of sense. 这很草率,可能没有任何意义。 I included both the main(String[] args) and my method because both are needed to set it up. 我包括main(String [] args)和我的方法,因为两者都需要进行设置。 When I run it I am getting the wrong answer and I am trying to troubleshoot by printing out the values(I have left these print lines in my code), The answer I'm getting is wrong. 当我运行它时,我得到了错误的答案,并且我试图通过打印出值来进行故障排除(我在代码中留了这些打印行),我得到的答案是错误的。 I belive it is something to do with how the elements of my array are being input. 我相信这与数组元素的输入方式有关。

The array I'm using is 23.5 35 2 10 4.5 3 45 3.5 35 44 5.5 9.6 and the answer I keep getting is (2,2) which is 5.5 if I am reading it correctly. 我正在使用的数组是23.5 35 2 10 4.5 3 45 3.5 35 44 5.5 9.6,而我一直得到的答案是(2,2),如果我正确阅读它的值是5.5。 The value of X is 3 and Y is 4. X的值为3,Y的值为4。

Any help as to why my code is returning the wrong answer would be greatly appreciated. 对于为什么我的代码返回错误答案的任何帮助,将不胜感激。 Thank you. 谢谢。

Edit: I added the values of X and Y. 编辑:我添加了X和Y的值。

You need to reassign the variable largest after you find a bigger value in the block: 在块中找到更大的值后,需要重新分配largest的变量:

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

Delete variables m and n , they are redundant. 删除变量mn ,它们是多余的。

I think the way you used the loops is right to find the largest number, the only problem is re-assignment of largest is not done in locateLargest function, 我认为您使用循环的方式是找到最大数目的正确方法,唯一的问题是在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;
}

The question has been answered but I think it's also worth you thinking about how you can solve this sort of problem without having to ask on Stack Overflow. 这个问题已经回答了,但我认为也值得您思考如何解决此类问题而不必询问Stack Overflow。 Even if your assignment does not ask for it I STRONGLY recommend you look at starting to do Test Driven Development (TDD) - http://www.javaworld.com/article/2073090/testing-debugging/getting-started-with-test-driven-development.html 即使您的作业没有要求,我也强烈建议您考虑开始进行测试驱动开发(TDD)-http: //www.javaworld.com/article/2073090/testing-debugging/getting-started-with-test -driven-development.html

In the absence of that at least try and "reduce your feedback loop". 在这种情况下,至少应尝试“减少反馈循环”。 For example the following code block allows you to easily (and visually) build test arrays that you can work with (rather than having to take user input every time you want to test your code). 例如,以下代码块使您可以轻松(直观地)构建可以使用的测试数组(而不是每次要测试代码时都必须接受用户输入)。

This would allow you to build 5 slightly different arrays in code and see which ones are failing which should help you work out why. 这将使您可以在代码中构建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();
        }

    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM