Page 2 of 2 FirstFirst 12
Results 21 to 27 of 27

Thread: 2D Array help

  1. #21
    Etimer is offline Member
    Join Date
    Feb 2012
    Posts
    35
    Rep Power
    0

    Default Re: 2D Array help

    have it return a 2D array of ints that have to be formatted for printing

  2. #22
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,305
    Rep Power
    25

    Default Re: 2D Array help

    That wouldn't work because it's a 2d array and it cant just loop through for some reason.
    x is a one dim array. following is line 3 in post #1
    Java Code:
     for(int[] x: day()){
    In post 13 you printed a one dim array:
    Java Code:
     for(int y: x){
                System.out.println(y);
            }
    Put the two together

  3. #23
    Etimer is offline Member
    Join Date
    Feb 2012
    Posts
    35
    Rep Power
    0

    Default Re: 2D Array help

    Like this?
    Java Code:
    public static void main(String[]args){
            for(int x: day()){
                System.out.println(x);
            }
        }
    Last edited by Etimer; 03-16-2012 at 02:43 AM.

  4. #24
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,305
    Rep Power
    25

    Default Re: 2D Array help

    In post #1 replace line 4 with the code from the end of post #22 that prints out a 1D array

    Java Code:
             for(int y: x){
                System.out.println(y);
            }

  5. #25
    Etimer is offline Member
    Join Date
    Feb 2012
    Posts
    35
    Rep Power
    0

    Default Re: 2D Array help

    Ohhhh! I didn't catch what you said at first but now i see.
    Sorry for practically making you spoon feeding me but i'll look over this and practice it until i understand it completely.
    Thank you so much!!
    And here's the final code:
    Java Code:
    public class b4 {   
        public static void main(String[]args){
            day();
        	for(int[]x: day()){
        		for(int y: x){
        		    System.out.println(y);
        		}
            }
        }
         
        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;
        }
    }
    Last edited by Etimer; 03-16-2012 at 02:57 AM.

  6. #26
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,305
    Rep Power
    25

    Default Re: 2D Array help

    Java Code:
        public static void main(String[]args){
            for(int[] x: day()){
    //            System.out.println(x);  // THIS IS LINE 4 replace it with the following:
                  for(int y: x){
                     System.out.println(y);
                  }
            }
        }

  7. #27
    Etimer is offline Member
    Join Date
    Feb 2012
    Posts
    35
    Rep Power
    0

    Default Re: 2D Array help

    I did that in #25, i just got confused because at the top of my code it says the package name, and i forgot that i didn't put that on here.

Page 2 of 2 FirstFirst 12

Similar Threads

  1. How to convert array of Objects into array of Strings
    By elenora in forum Advanced Java
    Replies: 1
    Last Post: 06-10-2011, 03:48 PM
  2. Replies: 0
    Last Post: 05-12-2011, 07:04 PM
  3. Replies: 23
    Last Post: 09-07-2010, 08:12 PM
  4. Replies: 2
    Last Post: 09-06-2010, 01:03 AM
  5. Replies: 1
    Last Post: 03-31-2009, 06:40 AM

Posting Permissions

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