没有添加到arraylist的值

时间:2013-05-14 02:29:45

标签: java function arraylist call

public static void main(String[] args)
    {
       List <int []> arrayList = new ArrayList<int []>();  // array list to hold child arrays
       PriorityQueue <state> open = new PriorityQueue<state>();  // priority queue for minimum huristics
       List<state> closed = new ArrayList<state>();     // create a list of states  to hold visited states
       int [] arr = new int  [9];
       for(int i = 0; i < 9; i++)
       {
           if(i == 1) arr[i] = 0;
           else
               arr[i] = i + 1;
       }

       state check = new state(arr,0,0,0);
      // for(int i = 0; i< 9; i++)
        //    System.out.println(arrayList.get(0)[i]);
       arrayList =  gen_next(closed,arrayList,check);
       int j = 0;
       int [] x = new int [9];
       System.out.println("From outside");
       for(int i = 1; i< 3;i++)
       {    

            x = (arrayList.get(i));
            for(int k = 0; k < 9; k++)
            System.out.print(x[k] + " ");
            System.out.println("");
             //System.out.println(x);
       }
}
public static List<int []> gen_next(List <state> closed,List <int []> next_arrlist,state current)
    {
      //  List <int []> next_arrlist = new ArrayList<int []>();
        int [] cur_board = current.get_board();
        int [] copy_arr = new int [cur_board.length];
        int hole = current.getHole();
        System.out.println(hole);

        System.out.println(hole);
        System.out.println("hello");
        copy(cur_board, copy_arr);
        if(hole == 0)
        {

            swap(copy_arr,hole,hole + 1);
            next_arrlist.add(copy_arr);
            for(int i = 0; i< 9;i++)
            System.out.print(next_arrlist.get(0)[i]);
            System.out.println("");

            copy(cur_board,copy_arr);

            swap(copy_arr,hole,hole + 3);
            next_arrlist.add(copy_arr);
            for(int i = 0; i< 9;i++)
            System.out.print(next_arrlist.get(0)[i]);
            System.out.println("");


            copy(cur_board,copy_arr);


        }
        else if (hole == 1)
        {
            swap(copy_arr,hole,hole - 1);
            next_arrlist.add(copy_arr);
            for(int i = 0; i< 9;i++)
            {
                System.out.print(copy_arr[i] +" "); 
            }
            System.out.println("");
            for(int i = 0; i< 9;i++)
                System.out.print(next_arrlist.get(0)[i]+" "); System.out.println("");
            copy(cur_board, copy_arr);
            swap(copy_arr,hole,hole + 1);     
            next_arrlist.add(copy_arr);
            copy(cur_board,copy_arr);
            swap(copy_arr,hole , hole + 3);
            for(int i = 0; i< 9;i++)
            {
                 System.out.print(copy_arr[i]+" ");
            }
            next_arrlist.add(copy_arr);
            System.out.println("");
            for(int i = 0; i< 9;i++)
            {
                 System.out.print(next_arrlist.get(1)[i]+" ");
            }
            copy(cur_board,copy_arr);
        }
        else if (hole == 2)
        {
            swap(copy_arr,hole , hole - 1);
            for(int i = 0; i<9;i++) System.out.print(copy_arr[i]+" ");
            System.out.println(""); // this value must be reflected in arraylist
            next_arrlist .add(copy_arr);
            for(int i = 0; i< 9;i++)
                System.out.print(next_arrlist.get(0)[i]+" "); System.out.println("");
            copy(cur_board,copy_arr);
            swap(copy_arr,hole, hole +3);
            for(int i = 0; i<9;i++) System.out.print(copy_arr[i]+" ");
            System.out.println("");

            next_arrlist.add(copy_arr);
            for(int i = 0; i< 9;i++)
                System.out.print(next_arrlist.get(0)[i]+" ");
            copy(cur_board,copy_arr);
        }
     return next_arrlist;
}

我编写了这段代码来从现有数组中生成新数组,并将每个数组存储在一个arraylist中,但是这里的arraylist只包含现有的数组,尽管在fub = nction中打印了arraylist的内容确实产生了正确的结果。

0 个答案:

没有答案