2014-11-15 34 views
0

我試圖將二維數組的元素複製到一維數組中。我知道我必須使用嵌套循環來複制元素,但我不知道從哪裏開始。這是我到目前爲止有:將二維數組的元素複製到一維數組中

import java.util.Scanner; 
import java.io.IOException; 

public class assignment{ 

    public static void main(String args[]) { 
     Scanner keyboard = new Scanner(System.in); 

     System.out.println("Welcome to Memory Game"); 

     int board_side;  

     do { 
      System.out.println("\n For 2x2 board game press 2"+ 
       "\n For 4x4 board game press 4"+ 
       "\n For 6x6 board game press 6"); 
      board_side=keyboard.nextInt(); 
     } while(board_side!=2 && board_side!=4 && board_side!=6); 

     char[][] board = createBoard(board_side); 

     shuffleBoard(board); 

     playGame(board); 
    } 




    public static void shuffleBoard(char[][] board) 
    { 
     int N = board.length*board.length; 
     char[] board1D = new char[N]; 

     // Copy the elements of 2D array into that 1D array here 

    } 

    public static char[][] createBoard(int side) 
    { 
     char[][] tmp = new char[side][side]; 
     int letter_count=0; 

     for (int row = 0; row < tmp.length/2; row++) { 
      for(int col = 0; col < tmp[row].length; col++) { 
       tmp[row][col]=(char)('A'+letter_count); 
       tmp[row+tmp.length/2 ][col]=tmp[row][col]; 
       letter_count++; 
      } 
     } 
     return tmp; 
    } 
} 

回答

0

假設其N * N矩陣,你可以這樣做:

int N = board.length*board.length; 
char[] board1D = new char[N]; 

int k = 0; 
for (int i =0; i<board.length; i++) { 
    for (int j =0; j<board.length; j++) { 
     board1D[k++] = board[i][j]; 
    } 
} 
0

,如果你有m行和n列,那麼你需要一個1-d陣列與m*n空間。

下面是示例代碼:

int k = 0; // counter 
int max_size = m*n; // maximum 1-D array size possible 

for(i = 0; i<m; i++) { // adjust your m and n values 
    for(j = 0; j<n; j++) { 
     if(k > max_size){ 
      // You need to consider what happens here - I simply put it as a stub 
      throw new IndexOutOfBoundsException("Index " + k + " is out of bounds!"); 
     } 
     target_matrix[k++] = source_matrix[i][j]; 
     } 
} 

這是假設你的索引偏好是由左到右,即每列第一。

請記住,嵌套for循環中的我的if語句已解決了諸如outofbounds異常等問題。我想確保您注意到OutOfBounds異常的機會。如果你覺得你不需要它,請刪除if block

0

是的,你有代碼在那裏,但你只需要一小段代碼來理解答案。我只是想了解如何將2維數組複製到1維數組中。未經測試的代碼和語法錯誤可能比比皆是。

// Presume TwoDArray contains [10][10] elements. 
// Presume OneDArray contains [100] elements. 

for (column = 0; column < 10; ++column) { 
    for (row = 0; row < 10; ++row) { 
     OneDArray[column*10 + row] = TwoDArray[column][row]; 
    } 
} 

山口* 10 +行(IE列倍行大小加上行)是魔術公式,從2D陣列爲一維數組轉換和實際上是一個2D陣列通常存儲在存儲器反正方式,通常。 (你可能想要仔細檢查,因爲它可能先是行,然後是列,我已經看到它在不同平臺上以不同語言做了不同的處理)。

0

嗨檢查什麼,我沒到目前爲止,我對第二個方法 http://pastebin.com/XDsp6ze7

// Family name, Given name: 
// Student number: 
// Course: IT1 1120 
// Assignment: 4 


import java.util.Scanner; 
import java.io.IOException; 

public class A4_8044047{ 

    // main method. DO NOT MODIFY 
public static void main(String args[]) { 
    Scanner keyboard = new Scanner(System.in); 

    System.out.println("Welcome to Memory Game"); 

    int board_side; 

    //this loop obtains the board size, or more specifically 
    // the length of the side of the board 
    do{ 
    System.out.println("\n For 2x2 board game press 2"+ 
    "\n For 4x4 board game press 4"+ 
    "\n For 6x6 board game press 6"); 
    board_side=keyboard.nextInt(); 
    }while(board_side!=2 && board_side!=4 && board_side!=6); 

    char[][] board = createBoard(board_side); 

    // a call the the shuffle method 
    shuffleBoard(board); 

    // a call to the game playing method 
    //playGame(board); 

} 




// The following method should shuffle the input 2D array caled board 
public static void shuffleBoard(char[][] board) 
{ 
    // This creates a 1D array whose size is equal to the size of the board 
    int N = board.length*board.length; 
    char[] board1D = new char[N]; 

    // Copy the elements of 2D array into that 1D array here 
    int a = 0; 
    for (int b=0;b<board.length;b++) 
    { 
    for (int c=0;c<board.length;c++) 
    { 
     board1D[a] = board[b][c]; 
     a++; 
    } 
    } 

    // Shuffle 1D array here 
    for (int d=0;d<board1D.length;d++) 
    { 
    int index=(int)(Math.random()* board1D.length); 
    char temp=board1D[d]; 
    board1D[d]=board1D[index]; 
    board1D[index]=temp; 
    } 
    for(int row2=0;row2<board1D.length;row2++) 
    { 
    //System.out.print(board1D[row2] + " "); 
    } 

    //Copy shuffled 1D array back into 2D array, i.e., back to the board 
    int M; 
    if (N==4) 
    { 
    M=2; 
    } 
    else if (N==16) 
    { 
    M=4; 
    } 
    else 
    { 
    M=6; 
    } 
    int count=0; 
    for (int h=0;h<M;h++) 
    { 
    for (int k=0;k<M;k++) 
    { 
     if (count==board1D.length) 
     { 
     break; 
     } 
     board[h][k]=board1D[count]; 
     count++; 
    } 
    } 

    /*for(int row2=0;row2<M;row2++) 
    { 
    for(int col2=0;col2<M;col2++) 
    { 
     System.out.print(board[row2][col2]+ " "); 
    } 
    System.out.println(); 
    }*/ 
} 



// a game playing method 
public static void playGame(char[][] board) 
{ 
    Scanner keyboard = new Scanner(System.in); 

    // this createst a 2D array indicating what locations are paired, i.e., discovered 
    // at the begining none are, so default initializaiton to false is ok 
    boolean[][]discovered=new boolean[board.length][board[0].length];; 


    // the code for your game playing goes here 

    Still working on this part any help would be nice email: [email protected] 






} 


    // createBoard method. DO NOT MODIFY! 
/* this method, createBoard, creates the board filled with letters of alphabet, 
    where each letter appears exactly 2 times 
    e.g., for 4 x 4, the returned board would look like: 
    A B C D 
    E F G H 
    A B C D 
    E F G H */  
public static char[][] createBoard(int side) 
{ 
    char[][] tmp = new char[side][side]; 
    int letter_count=0; 
    for (int row = 0; row < tmp.length/2; row++){ 
    for(int col = 0; col < tmp[row].length; col++) 
    { 
    tmp[row][col]=(char)('A'+letter_count); 
    tmp[row+tmp.length/2 ][col]=tmp[row][col]; 
    letter_count++; 
    } 
    } 
    return tmp; 
} 


    // waitForPlayer method. Do not modify! 
public static void waitForPlayer() 
{ 
    System.out.print("Press enter to continue"); 
    try { 
    System.in.read(); 
    } 
    catch (IOException e){ 
    System.out.println("Error reading from user"); 
    } 
} 

} 
+0

雖然此鏈接可以回答這個問題,最好是在這裏有答案的主要部件,並提供鏈接供參考。如果鏈接頁面更改,則僅鏈接答案可能會失效。 – Sasa