【问题标题】:Java. Array, save previous values爪哇。数组,保存以前的值
【发布时间】:2018-05-01 07:01:28
【问题描述】:

无法弄清楚如何做这件事。我需要在数组中存储和计算“跳跃”的数量和“跳跃”的方向。 控制台必须显示如下内容:

:跳转1,向左

:跳转 2,方向右

在我下面的代码中,当我打印数组元素时,int jump 总是等于 0;

我尝试了很多变化,有时我得到了(例如):

jump();

jump();

jump();

jump();

它会打印:{0,0,0,4},而不是 {1,2,3,4};

public class Jump {
int jump;
String direction;
Jump[] jumpArray = new Jump[100];
int storeJump;
String storeDirection;

public void jump() {
    jump++;
    if (jump > 50) {
        System.out.println("need a rest");
        return;
    } else {

        for (int i = 0; i < jumpArray.length; i++) {
            jumpArray[i] = new Jump();
        }
    }
    jumpArray[jump].storeJump = jump;
    jumpArray[jump].storeDirection = direction;

}

public static void main(String[] args) {
    Jump jumper = new Jump();
    jumper.jump();
    jumper.jump();

    for (int i = 0; i < jumper.jump; i++) {
        System.out.println(jumper.jumpArray[i].jump);

    }

}

}

【问题讨论】:

  • 在您的 jump() 方法中,您每次都替换所有 Jump 对象:jumpArray[i] = new Jump();,这可能不是您想要的。
  • 您只打印来自jumpArray 的元素,即Jump[]。在Jump#jump() 中,您为Jump#jump 调用的每个调用重复重新初始化数组,最后将jump 的增量值存储在Jump#storejump 中。但最后你打印jumpArray[i].jump,因为你从来没有在方法中为它赋值,所以总是0。此外,还有一些设计缺陷使其比应有的难度更大
  • 如果我可能会问这个问题:为什么类会跳转一个自身的数组 jumpArray 作为一个字段?这似乎是一个奇怪的设计。

标签: java arrays loops store


【解决方案1】:

我认为下面的代码可以解决你的问题,但我认为你应该改进你的代码。

int jump;
String direction;
Jump[] jumpArray = new Jump[100];
int storeJump;
String storeDirection;

public void jumping() {
    jump++;
    if (jump > 50) {
        System.out.println("need a rest");
        return;
    } else {
        jumpArray[jump] = new Jump();
    }
    jumpArray[jump].storeJump = jump;
    jumpArray[jump].storeDirection = direction;

}

public static void main(String[] args) {
    Jump jumper = new Jump();
    jumper.jumping();
    jumper.jumping();

    Set<Jump> mySet = new HashSet<Jump>(Arrays.asList(jumper.jumpArray));

    for (int i = 1; i < mySet.size(); i++) {
        System.out.println(jumper.jumpArray[i].storeJump);
    }
}

【讨论】:

  • 非常感谢!你真的帮了我很多!我现在对我很生气,因为我写的代码和你的类似(只是没有 HashSet),总是得到 NullPointerException,现在我看到了 int i=1;这就是我所要做的,因为 jump++ 递增,并且 array[0] =null,对吗?和数组 [1] = 值。就在我研究你的答案时,我想到了它。谢谢!
【解决方案2】:

我认为将jumpArray 移动到main() 会更有意义:

public class Jump {
    String direction;
    //int storeJump;   Not sure what this is for
    //String storeDirection;    Or this

    @Override
    public String toString() {
        return direction;
    }

    // Static function that creates an initializes a Jump object
    public static Jump jump(String direction) {
        Jump jump = new Jump();
        jump.direction = direction;
        return jump;
    }

    public static void main (String[] args)
    {
        Jump[] jumpArray = new Jump[100];
        int numJumps = 0;

        // A list of directions for testing
        String [] path = new String[]{"north", "south", "east", "west"};

        // Create a bunch of jumps
        for (String dir : path) {
            if (numJumps > 50) {
                System.out.println("need a rest");
                break;
            }
            // Create a new jump and add to the array
            jumpArray[numJumps++] = Jump.jump(dir);
        }

        // Print the jumps
        for (int i = 0; i < numJumps; i++) {
            System.out.println(i + " " + jumpArray[i]);
        }
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-12-27
    • 2011-01-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-06
    • 1970-01-01
    相关资源
    最近更新 更多