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 4 of 4

Thread: Array question

  1. #1
    Junior Member
    Join Date
    Aug 2011
    Posts
    20
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Array question

    I'm kind of ashamed to ask this question but can anybody tell me how I can print a particular value of the array in the main method? how do I print x[2], for example. Thanks.

    package pkgreturn;
     
    public class Return {
     
        public static void main(String args[]) {
     
     
        }
     
        public static int[] returnVal() {
            int x[] = {45, 8, 6, 5};
            return x;
     
        }
    }


  2. #2
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    987
    Thanks
    6
    Thanked 206 Times in 182 Posts

    Default Re: Array question

    how I can print a particular value of the array in the main method? how do I print x[2]
    You have to do two things: first get the array that returnVal() returns. This involves calling the method and assigning what it returns to a variable. There is no particular reason for that variable to be called x although it can be.

    Secondly you print the element of the array you are interested in.

    These two steps would be:

        public static void main(String[] args) {
            int[] arr = returnVal();
            System.out.println(arr[2]);
        }

    "int[] arr" and "int arr[]" mean the same thing, but I think the former is clearer: whether declaring variables, method arguments, or the return type of methods (where I don't think I have ever seen the other form used).

    These steps can be combined if that's sensible:

    public static void main(String[] args) {
        System.out.println(returnVal()[2]);
    }
    Last edited by pbrockway2; April 6th, 2012 at 10:58 PM.

  3. #3
    Junior Member
    Join Date
    Aug 2011
    Posts
    20
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Array question

    Quote Originally Posted by pbrockway2 View Post
    You have to do two things: first get the array that returnVal() returns. This involves calling the method and assigning what it returns to a variable. There is no particular reason for that variable to be called x although it can be.

    Secondly you print the element of the array you are interested in.

    These two steps would be:

        public static void main(String[] args) {
            int[] arr = returnVal();
            System.out.println(arr[2]);
        }

    "int[] arr" and "int arr[]" mean the same thing, but I think the former is clearer: whether declaring variables, method arguments, or the return type of methods (where I don't think I have ever seen the other form used).

    These steps can be combined if that's sensible:

    public static void main(String[] args) {
        System.out.println(returnVal()[2]);
    }
    Hello pbrockway2, thanks a lot for your answer.

    I was ashamed to ask because I knew how to do this in the past, but I totally forgot you need the '[]' next to the method to retrieve the value. For some reason I was typing a dot between returnVal() and x[3] , instead of 'returnVal()[2]'.

    Thanks

  4. #4
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    987
    Thanks
    6
    Thanked 206 Times in 182 Posts

    Default Re: Array question

    You're welcome.

Similar Threads

  1. 2 array question
    By w.kit in forum Java Theory & Questions
    Replies: 1
    Last Post: January 16th, 2012, 07:56 AM
  2. Array List question
    By Demetrius82 in forum Java Theory & Questions
    Replies: 4
    Last Post: June 3rd, 2011, 10:28 AM
  3. Array question. Stumped!
    By tjanuranus in forum What's Wrong With My Code?
    Replies: 3
    Last Post: April 5th, 2011, 02:35 PM
  4. 2D Array Question
    By gmorris1986 in forum Java Theory & Questions
    Replies: 2
    Last Post: June 18th, 2010, 11:40 AM
  5. A question about an array
    By faizana2006 in forum Collections and Generics
    Replies: 2
    Last Post: October 28th, 2009, 04:36 PM