Results 1 to 2 of 2

Thread: Converting 1-dimensional array to 2-dimensional

  1. #1

    Thread Starter
    New Member
    Join Date
    Sep 2007
    Posts
    12

    Converting 1-dimensional array to 2-dimensional

    Hi all,

    Working on an exercise, but can't figure it all out. This is the code I have written at the moment:

    Code:
    import javax.swing.*;
    import java.awt.*;
    
    public class Sudoku extends JFrame 
    {
    	Sudoku()
    	{
    		int s[][] = 
    		{ 
            		{ 0, 5, 6, 9, 1, 7, 8, 3, 2 }, 
            		{ 3, 9, 2, 6, 4, 8, 5, 1, 7 }, 
            		{ 1, 8, 7, 5, 3, 2, 9, 4, 6 },
            		{ 2, 7, 1, 4, 9, 5, 3, 6, 8 },
            		{ 8, 3, 5, 7, 6, 1, 4, 2, 9 },
           		{ 6, 4, 9, 8, 2, 3, 1, 7, 5 },
           		{ 7, 6, 4, 1, 5, 9, 2, 8, 3 },
           		{ 9, 1, 3, 2, 8, 6, 7, 5, 4 },
           		{ 5, 2, 8, 3, 7, 4, 6, 9, 1 }
           		};
    		
    		JButton[] b = new JButton[81];
    	        for ( int row = 0; row < s.length; row++ )                   
            	{        
    			for (int Kolom = 0; Kolom < int AantalKolommen = 9; Kolom++) 
    			{ 
    				for (int i=0; i<81; i++)
    				{
    					b[i] = new JButton(" ");
    					if (s[i][i]!=1)
    					{
    						b[i].setText (""+s[i]);
    					}
    				add(b[i]);
    				}
    			}
    		}
    	}
    
    	public static void main(String args[])
    	{
    		Sudoku s= new Sudoku();
    		s.setSize(600,600);
    		s.setLayout(new GridLayout(9,9));
    		s.setVisible(true);
    		s.setTitle("Sudoku");
    	}
    }
    As you can see, it's using a 2-dimensional array except JButton. The problem I'm dealing with is not getting this 1-dimensional array converted to 2-dimensional. Anyone knows how to get it done?

  2. #2
    Arabic Poster ComputerJy's Avatar
    Join Date
    Nov 2005
    Location
    Happily misplaced
    Posts
    2,513

    Re: Converting 1-dimensional array to 2-dimensional

    I hope this makes more sense. If you want my opinion, your code doesn't make any sense
    Code:
    // ... some code
            int[][] s = {{0, 5, 6, 9, 1, 7, 8, 3, 2}, {3, 9, 2, 6, 4, 8, 5, 1, 7}, {1, 8, 7, 5, 3, 2, 9, 4, 6}, {2, 7, 1, 4, 9, 5, 3, 6, 8}, {8, 3, 5, 7, 6, 1, 4, 2, 9}, {6, 4, 9, 8, 2, 3, 1, 7, 5}, {7, 6, 4, 1, 5, 9, 2, 8, 3}, {9, 1, 3, 2, 8, 6, 7, 5, 4}, {5, 2, 8, 3, 7, 4, 6, 9, 1}};
            for (int i = 0; i < s.length; i++) {
                for (int j = 0; j < s[i].length; j++) {
                    JButton btn = new JButton();
                    if (s[i][i] != 1) {
                        btn.setText(Integer.toString(s[i][j]));
                    }
                    add(btn);
                }
            }
    // ... some code
    "I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
    My Blog

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width