【问题标题】:ArrayList/LinkedList get First Index numberArrayList/LinkedList 获取第一个索引号
【发布时间】:2016-01-17 09:57:53
【问题描述】:

是否有可能摆脱ArrayList第一个索引的第一个数字?

这是一个例子:

里面有5个项目:

path = {0.5,5.0},{0.6,6.0},{0.7,7.0},{0.8,8.0},{0.9,9.0}

我想从{0.5,5.0} 中取出号码5.0...

我用path.get(0) 试过了,但它只给了我{0.5,5.0} 回来。

是否有可能在不获取所有其他数字的情况下从中获取5.0

【问题讨论】:

  • 您必须从列表中获取数组对象才能从中获取第二个 ([1]) 元素。

标签: java list arraylist processing indexof


【解决方案1】:

如果您的 ArrayList 包含数组,这就是要走的路

myList.get(0)[1] // You're getting the index 1 from the 1st array of your ArrayList

否则,如果包含其他ArrayList's

myList.get(0).get(1) // Same logic as above applied to the Collection

【讨论】:

  • 集合没有get(int)。
  • @JakubKaszycki ArrayList 被称为 Collection,因此它扩展了 AbstractCollection
  • Collection 没有方法 E get(int)。这是List的方法,我的意思是
  • @JakubKaszycki ArrayList is a Collection 我从来没有说过 Collection 有一个 get 方法......但无论如何。
  • 你刚才说“与上述相同的逻辑应用于集合”
【解决方案2】:

如果ArrayList 中的花括号实际上是brackets(它们可能是),您可以使用:

myArray.get(0)[index] 获取你想要的索引。在您的示例中是:

myArray.get(0)[1];

注意:如果您的ArrayList 元素也是ArrayList,那么您需要使用get(0).get(1) 而不是get(0)[1]

【讨论】:

  • 也工作过 :) 非常感谢
猜你喜欢
  • 1970-01-01
  • 2016-12-23
  • 2017-10-30
  • 2021-12-15
  • 2013-06-14
  • 2018-06-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多