简体   繁体   English

引用Java中的对象数组

[英]Reference to array of objects in java

Before I get onto my question, I'd really like someone to give me specific advice because I've been searching for a long time now but can't seem to find out correct answer. 在我提出问题之前,我真的很希望有人给我具体的建议,因为我已经搜索了很长时间,但是似乎找不到正确的答案。 I know it's basics but I am having issues. 我知道这是基本知识,但我遇到了问题。 This is what I have in my code: 这是我的代码中的内容:

private Interface ClassName; //initializing class that implements interface

public Interface methodName () {
/*This is where I need to make arraylist of objects
of my class and return whole class as reference, maybe something like this:*/
List<Interface> listName = new ArrayList<>(perhaps needs number of objects added?);
listName.add(ClassName);

return ClassName; //or however this should be done

and I guess once I call method I could simply use listName.get(number of object); 而且我猜一旦我调用方法,我就可以简单地使用listName.get(objectnumber);

Problem is, I would simply return list as reference, but my method requires me to only return class with said interface and I am not sure how to reference my class to that specific arraylist of objects and return it back in my method. 问题是,我只是返回列表作为引用,但是我的方法要求我仅返回具有所述接口的类,而且我不确定如何将类引用到该对象的特定数组列表并将其返回到我的方法中。

Thank you for your time. 感谢您的时间。 I'll try explaining in plain English. 我将尝试用简单的英语解释。 First of all, it's assigement so I can't change the ways methods are given. 首先,这是一种伪装,因此我无法更改给出方法的方式。 By the way, those were not actual variable names, just names to clarify what I am reffering to. 顺便说一下,这些不是实际的变量名,只是为了澄清我所引用的名称。 This is explanation: /** * * Gets the {@link ISystemRegisters} object of the current computer system. 这是解释:/ ** * *获取当前计算机系统的{@link ISystemRegisters}对象。 * * * * @return Object representing the system registers. * * * * @return代表系统寄存器的对象。 */ public ISystemRegisters getRegisters(); * /公共ISystemRegisters getRegisters();

So I have class with different interface than ISystemRegisters and this method it in that is supposed to hold arrayList of objects (here registers) and then return object reference in which that array is, as I understood it. 所以我有一个类,它的接口与ISystemRegisters不同,并且此方法所采用的方法应该是持有对象的arrayList(这里是寄存器),然后返回该数组所在的对象引用,据我所知。

If all you want to do is return your list from your method, then you should declare your method like this: 如果您要做的只是从方法中返回列表,则应该这样声明您的方法:

public List<Interface> methodName () { 
    ...
    return listName; // instead of returning ClassName
}

With the list as the return type. 以列表作为返回类型。

Side notes : 注意事项

  • your first line is confusing, you're not initializing anything (as pointed out by @Lucas) but just declaring a variable (a field here) of type Interface with ClassName as name (which should be className instead). 你的第一行是混乱的,你没有任何初始化(由@Lucas指出的),但只是声明类型的变量(这里是场) InterfaceClassName的名称(这应该是className来代替)。

  • No, you don't need to specify the number of items at your list's creation. 不,您不需要在列表创建时指定项目数。 This is different from arrays. 这与数组不同。

  • Your variable names are confusing, if you end with name , people will think the variable contains a name. 您的变量名称令人困惑,如果您以name结尾,人们会认为变量包含名称。 They are likely to expect it that they are String s. 他们可能期望它们是String

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM