简体   繁体   English

Java中的二维数组

[英]2-D Arrays in Java

For my assignment I must write a program that simulates a catapults projectile launch distance and print it to a table. 对于我的作业,我必须编写一个模拟弹射器弹丸发射距离并将其打印到表格的程序。 I have no understanding of 2-D arrays even after the class lesson and many other third party resources I have looked at. 即使在上完课以及其他我看过的第三方资源后,我也对二维数组一无所知。 Can someone please give me tips and help on converting my program to use a 2-D array? 有人可以给我提示和帮助我将程序转换为使用二维数组吗?

Class 1: tester class 第1类:测试人员类

public class CatapultTester{

    public static void main(String[] args){

        double [] velocity = {20,25,30,35,40,45,50};
        double [] degrees = {20,25,30,35,40,45,50};


        Catapult vars = new Catapult(velocity,degrees);

        double [] distance = vars.calcDistance();

        System.out.println("              Projectile Distance (feet)              ");
        System.out.printf("%s", "MPH");
        for (int i = 0; i < degrees.length; i++){
            System.out.printf("%5.0f %1s",degrees[i], "deg");
        }

        System.out.print("\n");
        System.out.println("==================================================================");

        for(int i = 0; i < distance.length; i++)
        {
            System.out.printf( "%2.0f ", velocity[i]);
            for(int f = 0; f < distance.length; f++)
            {

                System.out.printf("%8.1f ",  distance[i]);

            } 
            System.out.printf("\n");
        }
    }
}

Class 2: 第2类:

public class Catapult extends CatapultTester
{
    double [] velocity;
    double [] degrees;

    Catapult(double[] v, double[] d){
       velocity = v;
       degrees = d;
    }

    public double [] calcDistance(){

        double [] total = {0,0,0,0,0,0,0};

        for (int i = 0; i < degrees.length; i++){

            total[i] = (Math.pow(velocity[i]/2.237, 2) * Math.sin(2 * Math.toRadians(degrees[i]))/9.8);

        }

        return total;
    }
}

Overall I need to take the velocity and angle arrays and combine them into one 2-Dimensional array and they use the method calcDistance() to get a set of values and then use a loop to print them into a table as the output. 总的来说,我需要采用速度和角度数组,并将它们组合成一个二维数组,它们使用方法calcDistance()获得一组值,然后使用循环将它们打印到表中作为输出。

A 2d array is just a way to organize your data into a grid. 2d数组只是将数据组织到网格中的一种方法。 It is an array of arrays. 它是一个数组数组。 In your case, for every possible launch angle and velocity, you calculate the distance achieved. 对于您的情况,对于每个可能的发射角度和速度,您都可以计算出所达到的距离。 So if you used a two-dimensional array, you could, for example, treat one dimension as the angle and the other dimension as the velocity. 因此,如果使用二维数组,则可以例如将一个维视为角度,将另一个维视为速度。 Then store that particular computed distance there. 然后将特定的计算距离存储在那里。

For example, if I made a tic-tac-toe game, I might represent the game board by a 3x3 array. 例如,如果我制作了一个井字游戏,我可能会用3x3阵列代表游戏板。 And then I could indicate a player's move by doing something like board[row][column] = 'X'; 然后我可以通过执行board[row][column] = 'X';来表明玩家的动作board[row][column] = 'X'; where row and column are values in the range [0..2]. 其中, rowcolumn是[0..2]范围内的值。

In your case, you may not want to index into the array with the actual velocity and angle values because you'd have sparse arrays; 在您的情况下,您可能不希望使用实际的速度和角度值索引到数组中,因为您的数组很稀疏 that is, to store the distance for velocity = 50, you'd need an array of length >= 50 and many of those array slots would not be used. 也就是说,要存储速度= 50时的距离,您需要一个长度> = 50的数组,并且其中许多数组槽将不使用。 Moreover, your velocities and angles happen to be integers, but they could instead be floating point values with fractional portions. 此外,您的速度和角度碰巧是整数,但它们可能是带小数部分的浮点值。 So instead of doing distances[angle][velocity] , you would do distances[i][j] where i and j are indices into your degrees and velocities tables, respectively. 因此,您无需执行distances[angle][velocity] ,而是执行distances[i][j] ,其中ij分别是您的degrees表和velocities表的索引。

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

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