English 中文(简体)
我怎么回去一阵 objects?
原标题:How do I return an array of objects in java?
  • 时间:2010-04-01 05:20:37
  •  标签:
  • java

我怎么回去一阵 objects?

问题回答

确实,你只能将一系列的参比/em>退回到物体上——在 Java没有对物体本身进行实际评价。 说到这一点,回归一系列目标参考资料是容易的:

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

(关于此点,请将一份参引<>>转至一系列标注)

public Object[] myMethod() {
  Object[] objectArray = new Object[3];
  return objectArray;
}

简单、真实。

先前的答复完全解决了你的问题......只是为了另一个选择。 i) 建议你使用<代码>List: 可在ArrayList<Object>上查阅,以便:

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

Good;

你们拥有哪些数据结构?

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

举以下例子:

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]);
  }
}

审判

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

上文已经解释了回归物体阵列,但在大多数情况下,不需要。 简单地将目标阵列放在方法上,加以修改,并将从被点上反映出来。 java没有价值。





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签