Results 1 to 5 of 5

Thread: returning array

  1. #1
    aizen92 is offline Senior Member
    Join Date
    Nov 2010
    Posts
    155
    Rep Power
    5

    Default returning array

    is there a way to have an array method that returns an array
    like we have integer method that can return an int, does that apply to arrays?
    if yes how?

    i must use arrays only, i saw some way using arraylist put the problem is asking specifically for arrays only
    Last edited by aizen92; 01-08-2011 at 12:38 PM.

  2. #2
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,732
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by aizen92 View Post
    is there a way to have an array method that returns an array
    like we have integer method that can return an int, does that apply to arrays?
    if yes how?

    i must use arrays only, i saw some way using arraylist put the problem is asking specifically for arrays only
    Sure, the array type is just a type: a method can create an array, play with it and return it, e.g.

    Java Code:
    int[] myMethod(int n) {
       int[] array= new int[n]; // create an array
       for (int i= 0; i < array.length; i++) // play with it
          array[i]= i;
       return array; // and return it
    }
    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    aizen92 is offline Senior Member
    Join Date
    Nov 2010
    Posts
    155
    Rep Power
    5

    Default

    i have a question here, is the "int[] myMethod(int n)" like when we do in normal method the "public static void myMethod()" or different or what? and the n is the length of the array right?

    thanks in advance

  4. #4
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,732
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by aizen92 View Post
    i have a question here, is the "int[] myMethod(int n)" like when we do in normal method the "public static void myMethod()" or different or what? and the n is the length of the array right?

    thanks in advance
    It tells the compiler what the type of the value is that is returned by the method, e.g. 'void' == method doesn't return anything, 'int[]' == method returns an array of ints. Indeed, n is supposed to be the length of the array, just for the example I passed it in as a parameter.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    aizen92 is offline Senior Member
    Join Date
    Nov 2010
    Posts
    155
    Rep Power
    5

Similar Threads

  1. Help with returning
    By Da1dmoney in forum Eclipse
    Replies: 3
    Last Post: 12-15-2010, 03:38 AM
  2. Syntax error when returning an array
    By wantakill in forum New To Java
    Replies: 2
    Last Post: 09-03-2010, 07:39 PM
  3. Returning HexaDecimal Values to an Array???
    By stylishkishore in forum Advanced Java
    Replies: 5
    Last Post: 12-18-2009, 09:04 AM
  4. Returning An Array
    By elektronika in forum New To Java
    Replies: 2
    Last Post: 12-07-2009, 04:43 PM
  5. Returning array problem.
    By Chase in forum New To Java
    Replies: 4
    Last Post: 10-21-2008, 10:07 PM

Posting Permissions

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