为什么我要打印的int []数组被截断?

时间:2019-01-29 04:41:50

标签: java printing pass-by-reference hashset

我正在尝试为正在处理的该任务打印一个int []数组。我的代码如下:

import VacuumCleanerEnvironment.Constants;
import java.util.*;

/**
 * Agent Class
 * Student's Assignment
 */
public class Agent {
    private String action = null;
    public Agent() {

    }

    private boolean wallCheck(Set walls, int row, int col, int i){
        int[] temp = {row, col};
        if(i == 1){ // up
            temp[0]--;
        }else if(i == 2){ //right
            temp[1]++;
        }else if(i == 3){ // down
            temp[0]++;
        }else{ // left
            temp[1]--;
        }
        return walls.contains(temp);
    }

    private void addWall(Set walls, int row, int col, String dir){
        int[] temp = {row,col};
        if(dir == "up"){
            temp[0]--;
        }else if(dir == "left"){
            temp[1]--;
        }else if(dir == "right"){
            temp[1]++;
        }else if(dir == "down"){
            temp[0]++;
        }
        walls.add(temp);
    }

    /**
     * can return:
     *      "up", "right", "down", "left", "suck dirt", "stay idle"
     * @return String
     */
    public String action(boolean dirty, boolean bump,int row, int col){
        //System.out.println(Constants.SIZE);
        //System.out.println(Constants.initialDirtProb);
        //System.out.println(Constants.dirtyProb);
        //System.out.println(Constants.dirtSensorFailProb);
        //System.out.println(Constants.bumpSensorFailProb);
//        System.out.println("\nbump:" + bump);
//        System.out.println("dirty:" + dirty);
//        System.out.println("row:" + row);
//        System.out.println("col:" + col); 
        //int r = Constants.randomGen.getInt(4);


        Set<int[]> walls = new HashSet<int[]>();
//        int[] wallRoom = new int[2];

        if (dirty)
        {
            action="suck dirt";
        }
        else
        {
            if(bump == true){
                this.addWall(walls, row, col, action);

                System.out.print("Walls:" + walls);
                Iterator value = walls.iterator();
                while(value.hasNext()){
                    System.out.print("," + (int[])value.next());
                }
                System.out.println();
            }

            Random r = new Random();
            int i = r.nextInt(4);
            while(this.wallCheck(walls, row, col, i)){
                System.out.println("wallcheck: " + this.wallCheck(walls, row, col, i));
                i = r.nextInt(4);
            }


            if(i == 1) {
                action = "up";
            }
            else if(i == 2) {
                action = "right";
            }
            else if(i == 3) {
                action = "down";
            }
            else {
                action = "left";
            }

        }
     return action;
    }
}

我实际上有2个问题:1)wallCheck方法未运行,我认为这是因为int []值被保存为引用而不是值。如何检查我的HashSet是否包含特定的int []值? (用于在地图上定位墙壁)。 2)为什么当我打印出HashSet“墙”时,我的值会被切断。他们正在这样打印:

Walls:[[I@1515e375],[I@1515e375
Walls:[[I@4abd1f59],[I@4abd1f59
Walls:[[I@e4612e6],[I@e4612e6
Walls:[[I@6936b791],[I@6936b791

谢谢!

--------------------------------编辑-------------- -----------------

好的,我知道了问题出在哪里。就像Dave所说的那样,Java不能很好地打印出数组,所以我不得不自己打印出来。另外,我在内部声明了HashSet的action方法,这就是为什么只存储1个值的原因。

0 个答案:

没有答案