2012-12-04 18 views
0

我想解決着名的8難題,其中一個3 * 3方塊充滿了1個空插槽和8個數字,解決方法是將其返回到原始狀態。通過引用vs按值java

要做到這一點,我有一個「狀態」arraylist存儲數字0〜9,代表拼圖。

解決方案涉及產生許多可能的移動狀態,這意味着我保存所做的每一個合法移動和由此產生的難題。這是使用下面的方法完成的,但是我的swapAndStore不會每次都編輯原始傳遞的數組。相反,當在下面的genSuccessors()中調用時,它將對第一個if條件正常工作,然後對第一個if的結果應用next if條件。我認爲我通過製作一個名爲「oldBoard」的新益智遊戲來解決這個問題,以保存原始輸入難題供將來參考,但這也不起作用。一位朋友告訴我,這可能與我無法很好把握的參考問題有關。我知道當x = 0,y = 1時,java不會做交換(x,y),所以x = 1,y = 0,但是看不到這在這裏如何適用。 意見?

private void swapAndStore(int d1, int d2, ArrayList<State> s) 
{ 
    //int[] cpy = copyBoard(curBoard); 
    int[] cpy = new int [curBoard.length]; 
    System.arraycopy(curBoard,0,cpy,0,curBoard.length); 
    int[] oldBoard = new int [curBoard.length]; 
    System.arraycopy(curBoard,0,oldBoard,0,curBoard.length); 
    int temp = cpy[d1]; 
    cpy[d1] = cpy[d2]; 
    cpy[d2] = temp; 
    s.add((new State(cpy))); 
    curBoard = oldBoard; 
    System.out.println("swapandstore storing"); 
    s.get(s.size()-1).printState(); 

} 

public ArrayList<State> genSuccessors() 
{ 
    ArrayList<State> successors = new ArrayList<State>(); 
    int hole = getHole(); 

    // try to generate a state by sliding a tile leftwise into the hole 
    // if we CAN slide into the hole 
    if (hole != 0 && hole != 3 && hole != 6) 
    { 
     /* 
     * we can slide leftwise into the hole, so generate a new state for 
     * this condition and throw it into successors 
     */; 
     System.out.println("left"); 
     swapAndStore(hole - 1, hole, successors); 
    } 

    // try to generate a state by sliding a tile topwise into the hole 
    if (hole != 6 && hole != 7 && hole != 8) 
    { 
     System.out.println("top"); 
     swapAndStore(hole + 3, hole, successors); 
    } 

回答

1

Java是通過值 - 總是

基元通過價值傳遞;對象引用是按值傳遞的。

這意味着你不能改變參考值。但是在對象的情況下,你可以改變它們的狀態 - 如果它們是可變的。

所以,你可以這樣做:

package cruft; 

import java.io.PrintStream; 

/** 
* ArraySwapDemo description here 
* @author Michael 
* @link 
* @since 12/3/12 9:48 PM 
*/ 
public class ArraySwapDemo { 

    public static void main(String[] args) { 
     System.out.println(String.format("before: %s", arrayToString(args))); 
     swapValues(args, 0, args.length-1); 
     System.out.println(String.format("after : %s", arrayToString(args))); 
    } 

    // No checks for proper indexes, but you get the idea. 
    private static void swapValues(String[] args, int i, int j) { 
     String temp = args[i]; 
     args[i] = args[j]; 
     args[j] = temp; 
    } 

    public static String arrayToString(String [] array) { 
     StringBuffer buffer = new StringBuffer(1024); 
     buffer.append('{'); 
     for (int i = 0; i < array.length-1; ++i) { 
      buffer.append(array[i]).append(','); 
     } 
     buffer.append(array[array.length-1]).append('}'); 
     return buffer.toString(); 
    } 

} 

如果我有四根弦在命令行中運行它 - 富酒吧巴茲蝙蝠 - 我得到這樣的結果:

java cruft.ArraySwapDemo foo bar baz bat 
before: {foo,bar,baz,bat} 
after : {bat,bar,baz,foo} 

Process finished with exit code 0 
+0

ü可能想補充一下: TL;博士; :) – DarthVader

+0

我不知道我明白。 – duffymo

+1

@DarthVader說他有一個閱讀理解問題:D –