如何在Java中為arrayList創建get方法? - How to make a get method for an arrayList in Java? -开发者知识库

如何在Java中為arrayList創建get方法? - How to make a get method for an arrayList in Java? -开发者知识库,第1张

How do I make a get method for an ArrayList?

如何為ArrayList創建get方法?

I have the method header but I have no idea how to proceed.

我有方法標題但我不知道如何繼續。

 public Object get(int i) {

      }

1 个解决方案

#1


1  

What are you expecting to return from your get method? In java, ArrayList already has a get on it.. in C# also has a get defined on ArrayList but it is used differently.

您期望從get方法返回什么?在java中,ArrayList已經有了一個get ..它在C#中也有一個在ArrayList上定義的get,但它的使用方式不同。

I mean... if you are using an ArrayList this would be totally pointless and wrong... And i'm guessing your instructor wouldn't find it funny if you did:

我的意思是......如果你使用的是ArrayList,這將是毫無意義和錯誤的...而且我猜你的導師如果你這樣做會發現它很有趣:

public Object get(int i) {
     return myArrayList.get(i);
  }

or i mean you could beat around the bush and do this:

或者我的意思是你可以繞過灌木叢並做到這一點:

    public Object get(int i) {
Object[] myArray = new Object[myArrayList.size()];
myArray = myArrayList.toArray(myArray );
return myArray[i];
}

最佳答案:

本文经用户投稿或网站收集转载,如有侵权请联系本站。

发表评论

0条回复