Results 1 to 20 of 27
Thread: 2D Array help
- 03-16-2012, 12:04 AM #1
Member
- Join Date
- Feb 2012
- Posts
- 35
- Rep Power
- 0
2D Array help
Hey, I was wondering why this code isn't working. It's just printing random letters and numbers out instead.
I want for it to print all the days in order throughout the year, for example:
1
|
31
(February is excluded)
1
|
31
1
|
30
...and so on...
Here's the code:
Thanks!Java Code:public class b4 { public static void main(String[]args){ for(int[] x: day()){ System.out.println(x); } } public static int[][]day(){ int[]jan = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31}; //I'm skipping February for now, I know how to get the leap/non-leap year but for now I'll just skip it. int[]mar = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31}; int[]apr = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30}; int[]may = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31}; int[]jun = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30}; int[]jul = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31}; int[]aug = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31}; int[]sep = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30}; int[]oct = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31}; int[]nov = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30}; int[]dec = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31}; int[][]day = {jan, mar, apr, may, jun, jul, aug, sep, oct, nov, dec}; return day; } //Ignore this public static int[]feb(){ int[]feb = {}; return feb; } }
- 03-16-2012, 12:44 AM #2
Re: 2D Array help
Please post what the program is printing out.It's just printing random letters and numbers out instead.
Are you talking about the default value returned by the Object class's toString method for an array?
x is an array, not an single value
To format the contents of an array for printing see the Arrays class's toString() methodLast edited by Norm; 03-16-2012 at 12:46 AM.
- 03-16-2012, 12:53 AM #3
Member
- Join Date
- Feb 2012
- Posts
- 35
- Rep Power
- 0
Re: 2D Array help
Here's what the program prints out:
[I@316becfe
[I@2494310b
[I@7d1fdbef
[I@36a06816
[I@5d11985e
[I@2e2a730e
[I@64e7b3cf
[I@27ce0eca
[I@492d0430
[I@2a6e10d8
[I@2013eaab
- 03-16-2012, 12:57 AM #4
Re: 2D Array help
That is from the toString method. See post#2
- 03-16-2012, 01:04 AM #5
Member
- Join Date
- Feb 2012
- Posts
- 35
- Rep Power
- 0
Re: 2D Array help
Where would i put it?
- 03-16-2012, 01:07 AM #6
Re: 2D Array help
Where do you want to format the contents of the array referenced by x for printing?
- 03-16-2012, 01:11 AM #7
Member
- Join Date
- Feb 2012
- Posts
- 35
- Rep Power
- 0
Re: 2D Array help
I guess in the method where i declared the arrays.
- 03-16-2012, 01:18 AM #8
Re: 2D Array help
x is the array you are trying to print.
- 03-16-2012, 01:23 AM #9
Member
- Join Date
- Feb 2012
- Posts
- 35
- Rep Power
- 0
Re: 2D Array help
I'm totally lost
- 03-16-2012, 01:29 AM #10
Re: 2D Array help
x is an array. You will not see its contents when you try to print it with println. You get the String returned by the Object class's toString method.
You need to treat x as an array and access its members by indexing it: for example x[0] would give you the first element of the array.
Did you read the API doc for the Arrays class's toString method?
- 03-16-2012, 01:48 AM #11
Member
- Join Date
- Feb 2012
- Posts
- 35
- Rep Power
- 0
Re: 2D Array help
Yes i just did, after i saw your reply, but i don't really understand it. Its alright.
would i just do something like:
return day().toString;
- 03-16-2012, 01:52 AM #12
Re: 2D Array help
What happened when you tried that?return day().toString;
if x were defined like this:
int[] x = {1,2,3};
How would you print out its contents?
Write a small program to print it. Try this:
System.out.println(x);
Then write a loop to print the elements one by one.
- 03-16-2012, 01:57 AM #13
Member
- Join Date
- Feb 2012
- Posts
- 35
- Rep Power
- 0
Re: 2D Array help
Ok, heres the code that I ended up with after that:
And here's what it printed:Java Code:public class b4 { public static void main(String[]args){ System.out.println(day()); } public static String day(){ int[]jan = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31}; //I'm skipping February for now, I know how to get the leap/non-leap year but for now I'll just skip it. int[]mar = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31}; int[]apr = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30}; int[]may = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31}; int[]jun = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30}; int[]jul = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31}; int[]aug = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31}; int[]sep = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30}; int[]oct = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31}; int[]nov = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30}; int[]dec = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31}; int[][]day = {jan, mar, apr, may, jun, jul, aug, sep, oct, nov, dec}; return day.toString(); } //Ignore this public static int[]feb(){ int[]feb = {}; return feb; } }
[[I@19117681
And now i'll try what you just said.
EDIT:::
And heres how i would do that:if x were defined like this:
int[] x = {1,2,3};
How would you print out its contents?
Write a small program to print it. Try this:
System.out.println(x);
Then write a loop to print the elements one by one.
Java Code:public class b5 { public static void main(String[]args){ int[]x = {1,2,3}; for(int y: x){ System.out.println(y); } } }Last edited by Etimer; 03-16-2012 at 02:04 AM.
- 03-16-2012, 02:04 AM #14
Re: 2D Array help
If you want day() to return a String that prints like this:
1
2
3
etc
Then you will need to have loops that go through each of the arrays in the day() method and build a long String that looks like this:
"1\n2\n3\netc"
- 03-16-2012, 02:16 AM #15
Member
- Join Date
- Feb 2012
- Posts
- 35
- Rep Power
- 0
Re: 2D Array help
And i would put that in the parameters for the toString() function?
- 03-16-2012, 02:23 AM #16
Re: 2D Array help
If the day() method returns a String.
You would build a String and return it:
String aStr "1\n..."; // some how build the String you want
..
return aStr; // return the String to the caller of the day() method
- 03-16-2012, 02:25 AM #17
Member
- Join Date
- Feb 2012
- Posts
- 35
- Rep Power
- 0
Re: 2D Array help
so i wouldnt even be using the day array at all?
- 03-16-2012, 02:26 AM #18
Re: 2D Array help
Use the logic for printing the contents of array that you posted in #13
in the code in post#1 to print the contents of the array x.
- 03-16-2012, 02:28 AM #19
Member
- Join Date
- Feb 2012
- Posts
- 35
- Rep Power
- 0
Re: 2D Array help
That wouldn't work because it's a 2d array and it cant just loop through for some reason.
- 03-16-2012, 02:30 AM #20
Similar Threads
-
How to convert array of Objects into array of Strings
By elenora in forum Advanced JavaReplies: 1Last Post: 06-10-2011, 03:48 PM -
Display Array on another class after extracting from txt file and into array problem
By jonathan920 in forum NetBeansReplies: 0Last Post: 05-12-2011, 07:04 PM -
Variable of an object in an array compared to an element of another array?
By asmodean in forum New To JavaReplies: 23Last Post: 09-07-2010, 08:12 PM -
Trying to make an array list // inserting an element to middle of array
By javanew in forum New To JavaReplies: 2Last Post: 09-06-2010, 01:03 AM -
How to add an integer to a array element and the store that backinto an array.
By Hannguoi in forum New To JavaReplies: 1Last Post: 03-31-2009, 06:40 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks