5

How do I return an array of objects in java?

2
  • 3
    @peter i suppose return has only one meaning in programming. i.e. return some value from a function. DO you want to add anyother to the list. just kidding :D Commented Apr 1, 2010 at 6:26
  • I guess I didn't see what would be difficult about the actual return. The instantiation, perhaps. Commented Apr 1, 2010 at 14:14

7 Answers 7

13

Well, you can only actually return an array of references to objects - no expressions in Java actually evaluate to objects themselves. Having said that, returning an array of Object references is easy:

 public Object[] yourMethod()
 {
     Object[] array = new Object[10];
     // Fill array
     return array;
 }

(To be utterly pedantic about it, you're returning a reference to an array of object references.)

Sign up to request clarification or add additional context in comments.

2 Comments

Or to more succinctly emulate multiple value returns in eg. Lua: return new Object[] {expr1, expr2};
@TheMadsen That would work in Java too. I was aiming for making it easier to understand what's going on rather than brevity.
6
public Object[] myMethod() {
  Object[] objectArray = new Object[3];
  return objectArray;
}

Simple enough, really.

Comments

3

Answers posted earlier solves your problem perfectly ... so just for sake of another choice .. i will suggest you to use List : may be ArrayList<Object> so it could go as :

public List<Object> getListOfObjects() {
   List<Object> arrayList = new ArrayList<Object>();
   // do some work
   return arrayList;
}

Good luck;

3 Comments

No up-vote from me because you shouldn't really return a specified List implementation unless that implementation makes some guarantee paramount to your interface.
@Tim : I am so sorry but i am unable to get your point clearly. Am i missing something?
The Java Interfaces Collection, Set, List, etc... each make certain guarantees such as traversal order or limiting duplication. You specify a public API which specifically returns an ArrayList, but there is no rationale for why this public API must be an ArrayList and not just a List. What if your method implementation is sloppy and could benefit greatly from using some List implementation other than ArrayList? Future maintainers of your code could not simply plug in another List implementation because they are forced to abide by the contract of returning an ArrayList.
1

What is the data structure that you have?

  • If it is an array - just return it.
  • If it is a Collection - use toArray() (or preferably toArray(T[] a)).

Comments

0

Try the following example,

public class array
{
  public static void main(String[] args)
  {
    Date[] a = new Date [i];

    a[0] = new Date(2, "January", 2005);
    a[1] = new Date(3, "February", 2005);
    a[2] = new Date(21, "March", 2005);
    a[3] = new Date(28, "April", 2005);

    for (int i = a.length - 1; i >= 0; i--) 
    {
      a.printDate();
    }
  }
}

class Date
{
  int day;
  String month;
  int year;

  Date(int d, String m, int y)
  {
    day = d;
    month = m;
    year = y;
  }

  public void printDate()
  {
    System.out.println(a[i]);
  }
}

Comments

0

try this

public ArrayList<ObjectsType> fName(){
   ArrayList<ObjectsType>objectsList=new ArrayList<>();
   objectList.add(objets);
   return objectList;
}

Comments

-1

Returning an object array has been explained above, but in most cases you don't need to. Simply pass your object array to the method, modify and it will reflect from the called place. There is no pass by value in java.

3 Comments

There's no pass by reference in Java. Everything is passed by value, including references.
This kind of pass-through modification is considered bad practice anyway, I know this is the de facto thing to do in C and a lot of C++ programmers do it too but nope, this is Java, different paradigms, different best practices.
@Jon Skeet : Sorry my bad! that is what I meant! @Esko: Is that so? This is how I've seen most of the places so assumed that is the best practice! time to go back and check it out!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.