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

Thread: How do you return a array from a method, back into the main method that prints out stuff?

  1. #1
    Junior Member
    Join Date
    May 2014
    Posts
    5
    My Mood
    Confused
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default How do you return a array from a method, back into the main method that prints out stuff?

    Id like to know how to return a new array i wrote in a method below the main method. I want to print the array but system.out.print doesn't work for arrays apprently. Do you know what structure i should use?

    i think you use like somethingsomething. output ( [] arrray name)

    im not sure.

    thanks


  2. #2
    Member
    Join Date
    Oct 2013
    Location
    Manila, Philippines
    Posts
    285
    My Mood
    Amused
    Thanks
    6
    Thanked 64 Times in 61 Posts

    Default Re: How do you return a array from a method, back into the main method that prints out stuff?

    Id like to know how to return a new array i wrote in a method below the main method.
    just make a return type of array.
    Example:
    public int[] myIntArray() {
         int[] myArr = new int[3]; // creating an array with length of 3
         // assigning desired value for each element
         myArr[0] = 1;
         myArr[1] = 2;
         myArr[2] = 3;
         return myArr;
    }
    example codes above will return an array of int.
    note: you can also create a method above your main method. the order/place of methods in your class doesn't matter.

    I want to print the array but system.out.print doesn't work for arrays apprently.
    you can iterate the array from first element to last element (or vice versa) and print it.
    Example:
    public static void main(String[] args) {
          int[] myArr = {1, 2, 3}; //creating an array
          // printing it using enhance loop
          System.out.println("Printing array using enhance loop");
          for (int i : myArr) {
                System.out.println(i);
          }
     
          // printing array using index of element
          System.out.println("Printing array using index");
          for (int i = 0; i < myArr.length; i++) {
                System.out.println(myArr[i]);
          }
    }

    And lastly, I suggest you to read this Arrays (The Java™ Tutorials > Learning the Java Language > Language Basics)

  3. The Following User Says Thank You to dicdic For This Useful Post:

    GregBrannon (May 28th, 2014)

Similar Threads

  1. Replies: 2
    Last Post: May 27th, 2014, 12:36 PM
  2. Replies: 3
    Last Post: December 13th, 2013, 12:19 PM
  3. Why cant I call on the array I returned in the main method?
    By ColeTrain in forum What's Wrong With My Code?
    Replies: 3
    Last Post: November 16th, 2012, 04:08 PM
  4. Return letter grade to main method
    By jmanv888 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: October 15th, 2012, 04:48 PM
  5. method to return the position of the smallest item in an array
    By izzahmed in forum Java Theory & Questions
    Replies: 5
    Last Post: November 4th, 2011, 04:18 AM