Talk About Network

Google


Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Programming > Java Help > Re: iterating t...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 9 of 11 Topic 15994 of 16566
Post > Topic >>

Re: iterating through an array of String's

by Ian Kidder <ikidder@[EMAIL PROTECTED] > Apr 22, 2008 at 06:44 AM

On Apr 20, 4:50 am, thufir <hawat.thu...@[EMAIL PROTECTED]
> wrote:
> For lab2 athttp://members.shaw.ca/Java2611/I
need to load data (which
> should be an array of Strings) from DATA -- that's not the assignment,
> but it's a requirement that the data is loaded in this particular
> fa****on.  It's supposed to not even really be a problem, because we're
> supposed to (from my notes):
>
> hard code the data in the program,
> private final String[] GUEST_DATA = {...}
>
> use the constructor
>
> Guest guest1 = new Guest(GUEST_DATA[0]); // will grab row 0
>
> At the moment, I'd be happy to print out DATA, but I want to be able to
> iterate through DATA, assigning one row at a time to the data array,
> basically doing what's above.
>
> Yes, I'll be reviewing arrays, and it sounds straightforward, but I
don't
> see why the output is hexadecimal (the object's memory address?).
>
> Any "pointers" would be appreciated:
>
> thufir@[EMAIL PROTECTED]
 javac ArrayOfStrings.java
> thufir@[EMAIL PROTECTED]
 java ArrayOfStrings
> [[Ljava.lang.String;@[EMAIL PROTECTED]
> [[Ljava.lang.String;@[EMAIL PROTECTED]
> thufir@[EMAIL PROTECTED]
 cat ArrayOfStrings.java
> public class ArrayOfStrings
> {
>
>         public static String[] data;
>
>         private final static String[][] DATA = {
>                 {"00", "01", "02", "03"},
>                 {"10", "11", "12", "13"},
>                 {"20", "21", "22", "23"},};
>
>         public static void loadData() {
>                 for (int i=0; i<2; i++) {
>                         data = DATA[0];
>                         System.out.println(DATA);
>                 }
>         }
>
>         public static void main (String[] args) {
>                 loadData();
>         }}
>
> thufir@[EMAIL PROTECTED]
>
> thanks,
>
> Thufir

if only i knew a good mentat joke . . .

you are getting memory pointers from the run because you only have one
for loop and you are walking through a 2-d array. the output is
basically DATA[0], DATA[1]. to walk through a two dimensional array,
you need nested for loops. try this:

 public class ArrayOfStrings34
{

        public static String data;

        private final static String[][] DATA = {
                {"00", "01", "02", "03"},
                {"10", "11", "12", "13"},
                {"20", "21", "22", "23"},};

        public static void loadData() {
                for (int i=0; i<DATA.length; i++) {
                  for (int j = 0; j < DATA[0].length; j++) {
                    System.out.println(DATA[i][j]);
                  }
                }
        }

        public static void main (String[] args) {
                loadData();
        }


}
 




 11 Posts in Topic:
iterating through an array of String's
thufir <hawat.thufir@[  2008-04-20 08:50:00 
Re: iterating through an array of String's
Roedy Green <see_websi  2008-04-20 10:04:03 
Re: iterating through an array of String's
Thufir <hawat.thufir@[  2008-04-30 03:43:18 
Re: iterating through an array of String's
Roedy Green <see_websi  2008-04-20 10:05:35 
Re: iterating through an array of String's
voorth <voorth@[EMAIL   2008-04-21 03:51:03 
Re: iterating through an array of String's
Lew <lew@[EMAIL PROTEC  2008-04-21 07:12:00 
Re: iterating through an array of String's
thufir <hawat.thufir@[  2008-04-21 07:58:23 
Re: iterating through an array of String's
thufir <hawat.thufir@[  2008-04-21 10:34:52 
Re: iterating through an array of String's
Ian Kidder <ikidder@[E  2008-04-22 06:44:11 
Re: iterating through an array of String's
thufir <hawat.thufir@[  2008-04-23 04:06:25 
Re: iterating through an array of String's
Lew <lew@[EMAIL PROTEC  2008-04-23 00:19:01 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Sat Nov 22 16:28:46 CST 2008.