使用数组的Java [英] Using Arrays Java

查看:88
本文介绍了使用数组的Java的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题是:


  • 这是code正确,是否已正确分配给车轮转速和持续时间

  • 我需要什么,以增加运行code的主要方法做

  • 使用for循环通过每个行迭代,指示雀机器人根据每行中的数字移动。使用code JOptionPane.showMessageDialog(NULL,点击确定继续...)每一个动作后暂停;

公共类FinchArray {

 公共静态最终诠释LEFT_WHEEL = 0;
公共静态最终诠释RIGHT_WHEEL = 1;
公共静态最终诠释期限= 2; {
    INT [] []×=新INT [10] [3]; //二维数组填充零
    INT时间= 25;
    的for(int []行:X){
        行[2] =时间;
        时间+ = 25;
    }    的for(int []行:X){
        行[时间] =时间;
        时间+ = 25;
    }    的for(int []行:X){
        行[LEFT_WHEEL] =时间;
        时间+ = 25;
    }    的for(int []行:X){
        行[RIGHT_WHEEL] =时间;
        时间+ = 25;
    }    芬奇fRobot =新芬奇();    fRobot.setWheelVelocities(LEFT_WHEEL,RIGHT_WHEEL,持续时间);
    没过多久= System.currentTimeMillis的();
    而(System.currentTimeMillis的() - 前与LT 5000){         如果(fRobot.isObstacle())破; {        }
            fRobot.stopWheels();
            fRobot.quit();    }}


解决方案

您可以初始化二维数组(其中,在Java中,其实只是一维数组的数组)是这样的:

  INT [] [] X = INT新[10] [3]; //二维数组填充零
INT时间= 25;
的for(int []行:X){
    行[2] =时间;
    时间+ = 25;
}

这里使用增强循环以及相当于这样的:

  INT [] [] X = INT新[10] [3]; //数组以零填充
INT时间= 25;
的for(int i = 0; I< x.length ++我){
    INT []行= X [I]
    行[2] =时间;
    时间+ = 25;
}

请注意,此离开的左右车轮速度在零为其缺省值。

您当前code首先创建一个10x3二维数组,但你的循环重新分配各行是一个新的10个元素的数组,所以你结束了一个10×10阵列(这是不是你想要的)

P.S。使用3个元素的数组重新present三个不同的数据块并不是一个良好的编程风格(虽然它会工作得很好)。略有改善可以通过使用符号常量为下标来获得。声明在类的顶部:

 公共静态最终诠释LEFT_WHEEL = 0;
公共静态最终诠释RIGHT_WHEEL = 1;
公共静态最终诠释期限= 2;

然后使用这些索引到的每一行:

 的for(int []行:X){
    行[时间] =时间;
    时间+ = 25;
}

由于各行重新presents三种不同的属性,更好的方法是定义一个单独的类所包含的数据:

 公共类{步
    公众诠释leftWheelSpeed​​;
    公众诠释rightWheelSpeed​​;
    公众诠释持续时间;
}

然后,而不是 INT 值的二维数组,你可以声明步骤对象的一维数组:

 步骤[] X =新的一步[10];
INT时间= 25;
对于(步骤第一步:X){
    step.duration =时间;
    时间+ = 25;
}

My question is:

  • Is this code correct and is it properly assigned to the Wheel speeds and duration
  • What do I need to do in order to add the main method to run the code
  • use a for loop to iterate through each row, instructing the Finch robot to move according to the numbers in each row. Pause after each movement using the code JOptionPane.showMessageDialog(null,"Click OK to continue...");

public class FinchArray {

public static final int LEFT_WHEEL = 0;
public static final int RIGHT_WHEEL = 1;
public static final int DURATION = 2;{


    int[][] x = new int[10][3]; // 2D array filled with zeros
    int time = 25;
    for (int[] row : x) {
        row[2] = time;
        time += 25;
    }

    for (int[] row : x) {
        row[DURATION] = time;
        time += 25;
    }

    for (int[] row : x){
        row[LEFT_WHEEL] = time;
        time += 25;
    }

    for (int[] row : x){
        row[RIGHT_WHEEL] = time;
        time += 25;
    }

    Finch fRobot = new Finch();

    fRobot.setWheelVelocities(LEFT_WHEEL,RIGHT_WHEEL,DURATION);
    long before = System.currentTimeMillis();
    while (System.currentTimeMillis() - before < 5000){

         if(fRobot.isObstacle())break; {

        }
            fRobot.stopWheels();
            fRobot.quit();

    }





}

解决方案

You can initialize your 2D array (which, in Java, is really just an array of 1D arrays) like this:

int[][] x = new int[10][3]; // 2D array filled with zeros
int time = 25;
for (int[] row : x) {
    row[2] = time;
    time += 25;
}

This uses an enhanced for loop and is equivalent to this:

int[][] x = new int[10][3]; // array filled with zeros
int time = 25;
for (int i = 0; i < x.length; ++i) {
    int[] row = x[i];
    row[2] = time;
    time += 25;
}

Note that this leaves the left and right wheel speeds at their default values of zero.

Your current code is first creating a 10x3 2D array, but then your loop is reassigning each row to be a new 10-element array, so you end up with a 10x10 array (which is not what you want).

P.S. Using a 3-element array to represent three distinct pieces of data is not a good programming style (although it will work just fine). A slight improvement can be obtained by using symbolic constants for the subscripts. Declare at the top of your class:

public static final int LEFT_WHEEL = 0;
public static final int RIGHT_WHEEL = 1;
public static final int DURATION = 2;

Then use these to index into each row:

for (int[] row : x) {
    row[DURATION] = time;
    time += 25;
}

Since each row represents three different attributes, a better approach would be to define a separate class to contains the data:

public class Step {
    public int leftWheelSpeed;
    public int rightWheelSpeed;
    public int duration;
}

Then instead of a 2D array of int values, you can declare a 1D array of Step objects:

Step[] x = new Step[10];
int time = 25;
for (Step step : x) {
    step.duration = time;
    time += 25;
}

这篇关于使用数组的Java的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆