Results 1 to 4 of 4
Thread: Filling 2D Array
- 11-12-2008, 11:36 AM #1
Member
- Join Date
- Oct 2008
- Location
- Aberystwyth
- Posts
- 55
- Rep Power
- 0
Filling 2D Array
Hi all, I'm having a little trouble getting my head around
I'm not entirely sure what its telling me to do.Java Code:static void fill(char[] a, char val)]
This is the section of code containing this:
From what i've gather i type "char[]" then the name of array im filling "twoDimGrid" then i use a comma and enter char val. So i type "char ' '" However i get errors on eclipse saying:Java Code:public static class Grid { Grid[][] twoDimGrid = new Grid[31][31]; // 31x31 grid private static void fill(char[] twoDimGrid, char ' '); void print(){System.out.println(this.twoDimGrid);} }
Syntax error on token "' '", invalid VariableDeclaratorld
I have no idea what this means, if i cant use a space as a char what else should i use for my grid?
- 11-12-2008, 12:06 PM #2
Member
- Join Date
- Oct 2008
- Location
- Aberystwyth
- Posts
- 55
- Rep Power
- 0
i managed to get it t compile with no errors, however it does not display the array, any ideas why?
Java Code://define SnakeGame class public class SnakeGame { char s = ' '; //create main method(All Java prgrams need one to run ;)) public static void main(String[] args) { } //Create a 2D grid for the game to be played on public static class Grid { Grid[][] grid = new Grid[31][31]; // 31x31 grid static void fill(char grid[][], char _){} void print(){System.out.println(this.grid);} } }
- 11-12-2008, 12:31 PM #3
something like this
It goes something like this.I'm not proficient on the enhanced for loop, probably have one or more blunders but it is something like this.
Java Code:// preliminary public class SnakeGame { //create main method(All Java prgrams need one to run ;)) public static void main(String[] args) { Grid grid = new Grid(); grid.fill();// grid.print();// } //Create a 2D grid for the game to be played on public class Grid { char s = ' '; char [][] grid = new char[31][31]; // 31x31 grid void fill() { for( char[] row : grid ) { for( char row : column ) { column = s; } } } void print() { for( char[] row : grid ) { for( char row : column ) { System.out.println(column); } } } } }Introduction to Programming Using Java.
Cybercartography: A new theoretical construct proposed by D.R. Fraser Taylor
- 11-12-2008, 12:43 PM #4
Member
- Join Date
- Oct 2008
- Location
- Aberystwyth
- Posts
- 55
- Rep Power
- 0
A massive thanks for this, i can see where your coming from now and I'm beginning to understand it better, however that code produced 7 errors. :s
i would try and sort them but i dont understand. :sJava Code:SnakeGame2.java:7: non-static variable this cannot be referenced from a static context Grid grid = new Grid(); ^ SnakeGame2.java:20: row is already defined in fill() for( char row : column ) ^ SnakeGame2.java:20: cannot find symbol symbol : variable column location: class SnakeGame2.Grid for( char row : column ) ^ SnakeGame2.java:22: cannot find symbol symbol : variable column location: class SnakeGame2.Grid column = s; ^ SnakeGame2.java:30: row is already defined in print() for( char row : column ) ^ SnakeGame2.java:30: cannot find symbol symbol : variable column location: class SnakeGame2.Grid for( char row : column ) ^ SnakeGame2.java:32: cannot find symbol symbol : variable column location: class SnakeGame2.Grid System.out.println(column); ^
Similar Threads
-
Array help
By EternalSolitude in forum New To JavaReplies: 9Last Post: 10-28-2008, 07:10 AM -
3 by 3 array
By fromme87 in forum New To JavaReplies: 5Last Post: 10-02-2008, 08:34 AM -
Beginner Java graphics - filling concentric circles with color
By GenkiSudo in forum New To JavaReplies: 4Last Post: 09-13-2008, 11:07 AM -
Array Reflection: Multi Array Reflection
By Java Tip in forum java.langReplies: 0Last Post: 04-23-2008, 08:08 PM -
Would appreciate your help with 2d Array..
By cloudkicker in forum New To JavaReplies: 1Last Post: 02-11-2008, 02:34 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks