简体   繁体   中英

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

The element returned with the method queue.poll() is a Array<IntArray> , not an IntArray . When you call queue.poll()[0] you are getting the first element of that Array<IntArray> , so an IntArray . If you want to get the first element of the IntArray , you should call 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())

Furthermore, since you said you expected it to print 3 , remember that poll() removes the first element, not the last one. If you want to remove the last one, you can call pollLast() instead of poll() .

Seems like i was putting an Array of Array in the queue

the correct code should be

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

Now I am getting 1 as expected

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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