首页 > 解决方案 > Kotlin Queue returning object

问题描述

I am trying to create a queue of List

so far I have this

var queue = LinkedList<Array<IntArray>>()
queue.add(arrayOf(intArrayOf(1,2,0)))
queue.add(arrayOf(intArrayOf(3,4,5)))
Log.d("debugVal",queue.poll()[0].toString())

It returns something like this

D/debugVal: [I@81fc7ad

I was expecting it to print 1

I think it's returning an object. Can someone please tell me how to retrieve the list values from the polled element I need all three of the values from each polled element

标签: androidkotlinqueue

解决方案


使用该方法返回的元素queue.poll()是 a Array<IntArray>,而不是IntArray。当你打电话时,queue.poll()[0]你得到了第一个元素Array<IntArray>,所以一个IntArray. 如果要获取 的第一个元素IntArray,则应调用queue.poll()[0][0]

var queue = LinkedList<Array<IntArray>>()
queue.add(arrayOf(intArrayOf(1, 2, 0)))
queue.add(arrayOf(intArrayOf(3, 44, 10)))
Log.d("debugVal", queue.poll()[0][0].toString())

此外,既然您说您希望它打印3,请记住poll()删除第一个元素,而不是最后一个元素。如果要删除最后一个,可以调用pollLast()而不是poll().


推荐阅读