Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Question on arrays.

  1. #1
    Junior Member
    Join Date
    Apr 2011
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Question on arrays.

    Are arrays supposed to be unreadable? I have a program that sets words to each element of an array but when i try to print it out it just comes up in system code?

    Also - If I wanted to write said array into a file, would it then be readable? Or not?

    Thanks


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Question on arrays.

    There's no toString() method for arrays defined to print out all the elements. You must manually print out the contents of the array yourself.

    int a[] = {1,2,3,4};
    for(int i = 0; i < a.length; ++i) // prints out the contents of a
    {
        System.out.println(a[i]);
    }

    Calling the default toString() method of an array would call the Object toString() method, which prints out the hashCode and Object type. Neither of these have anything to do with the data being stored.

  3. #3
    Junior Member
    Join Date
    Apr 2011
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Question on arrays.

    Quote Originally Posted by helloworld922 View Post
    There's no toString() method for arrays defined to print out all the elements. You must manually print out the contents of the array yourself.

    int a[] = {1,2,3,4};
    for(int i = 0; i < a.length; ++i) // prints out the contents of a
    {
        System.out.println(a[i]);
    }

    Calling the default toString() method of an array would call the Object toString() method, which prints out the hashCode and Object type. Neither of these have anything to do with the data being stored.
    Okay great, thanks

    One other question - as ive declared my array within the for loop - how can i use it elsewhere in my program? I tried dimming as public but that did not compile

  4. #4
    Member
    Join Date
    Dec 2009
    Location
    UK
    Posts
    58
    My Mood
    Sleepy
    Thanks
    2
    Thanked 3 Times in 2 Posts

    Default Re: Question on arrays.

    Don't declare it *in* your loop, but at the beginning of your program. For instance, if you're using an array of type int, and your loop counts from zero to 11 (meaning 12 possible entries in the array, as it counts from zero), then you could do something like:

    public static int thisIsMyArray [11];

    at the beginning of your program.

    It might need some default values in there, if so, just enter some test data, like:

    public static int thisIsMyArray [11]=
    {
      0,1,2,3,4,5,6,7,8,9,10,11
    };

    It's something close to that - don't just copy and paste and assume that I'm correct, you'll have to test it as I sometimes get mixed up with different languages.

    Regards,

    Shaun.

  5. #5
    Junior Member
    Join Date
    Mar 2011
    Posts
    17
    Thanks
    1
    Thanked 6 Times in 6 Posts

    Default Re: Question on arrays.

    While thats mostly correct, when you declare the array you put the length in the brackets, a 12 item array(indices 0-11) would be declared
    int[] myArray = new int[12];

    To explain why it didn't compile, since you declared the array inside of the loop it had loop local scope, meaning each time through the loop it destroyed and created a new array, after finishing the loop it again destroyed the array. From there on if you try to reference it you will be given a compile time error since the array no longer exists.
    Last edited by sunde; April 24th, 2011 at 02:50 AM.

Similar Threads

  1. 2d arrays help
    By gonfreecks in forum What's Wrong With My Code?
    Replies: 0
    Last Post: November 27th, 2010, 05:27 PM
  2. arrays
    By dwamalwa in forum Java Theory & Questions
    Replies: 4
    Last Post: November 27th, 2010, 01:44 AM
  3. Arrays
    By mlan in forum Java Theory & Questions
    Replies: 2
    Last Post: February 26th, 2010, 10:23 AM
  4. Arrays: question about "removing" items.
    By sanfor in forum Collections and Generics
    Replies: 2
    Last Post: November 30th, 2009, 04:09 AM
  5. 2d Arrays
    By mgutierrez19 in forum Collections and Generics
    Replies: 5
    Last Post: October 27th, 2009, 04:08 PM