Why arraylist doesn't return insertion order in Kotlin?

假装没事ソ 提交于 2020-02-08 10:03:06

问题


I am facing a weird problem here.

I have an Array List in my fragment, and each time I add value to it I was expecting it to keep the insertion order. However, it doesn't keep this order when I print the arraylist, it gives a weird order which makes no sense at all.

Is there something I am missing here regarding ArrayList, or Fragments in Kotlin or Android?

Thanks in advance.

Below part of my codes;

class HourlySalesFragment : Fragment(){


var tt = arrayListOf<Double>()

override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,savedInstanceState: Bundle? ): View? { 

  // some code here


    val query08 = rootRef.child("Sales").orderByChild("time").startAt("08:00:00").endAt("08:30:00")
    val query0830 = rootRef.child("Sales").orderByChild("time").startAt("08:30:00").endAt("09:00:00")
    val query09 = rootRef.child("Sales").orderByChild("time").startAt("09:00:00").endAt("09:30:00")
    val query0930 = rootRef.child("Sales").orderByChild("time").startAt("09:30:00").endAt("10:00:00")
  //some other code here

    fun useArray(nt : String){
        println("ttt useArray $nt "+tt)

    }

    val valueEventListener08 = object : ValueEventListener {
         //some code here

        override fun onDataChange(p0: DataSnapshot) {
            if (p0.exists()) {
                for(ds in p0.children) {
           //some code here
                }

                tt.add(8.0)
                useArray("8")

            }

        }

        override fun onCancelled(databaseError: DatabaseError) {
            Log.d(TAG, databaseError.getMessage()) //Don't ignore errors!
        }
    }

    val valueEventListener08_30 = object : ValueEventListener {
         //some code here

        override fun onDataChange(p0: DataSnapshot) {
            if (p0.exists()) {
                for(ds in p0.children) {
           //some code here
                }

                tt.add(8.30)
                useArray("8:30")

            }

        }

        override fun onCancelled(databaseError: DatabaseError) {
            Log.d(TAG, databaseError.getMessage()) //Don't ignore errors!
        }
    }
    val valueEventListener09 = object : ValueEventListener {
         //some code here

        override fun onDataChange(p0: DataSnapshot) {
            if (p0.exists()) {
                for(ds in p0.children) {
           //some code here
                }

                tt.add(9.0)
                useArray("9")

            }

        }

        override fun onCancelled(databaseError: DatabaseError) {
            Log.d(TAG, databaseError.getMessage()) //Don't ignore errors!
        }
    }

    val valueEventListener09_30 = object : ValueEventListener {
         //some code here

        override fun onDataChange(p0: DataSnapshot) {
            if (p0.exists()) {
                for(ds in p0.children) {
           //some code here
                }

                tt.add(9.30)
                useArray("9:30")

            }

        }

        override fun onCancelled(databaseError: DatabaseError) {
            Log.d(TAG, databaseError.getMessage()) //Don't ignore errors!
        }
    }

This is my print out results

2020-01-26 13:49:32.431 744-744/com.soyut.su.epos01 I/System.out: ttt useArray 9 [9.0]
2020-01-26 13:49:32.444 744-744/com.soyut.su.epos01 I/System.out: ttt useArray 8:30 [9.0, 8.3]
2020-01-26 13:49:32.447 744-744/com.soyut.su.epos01 I/System.out: ttt useArray 9.30 [9.0, 8.3, 9.3]
2020-01-26 13:49:32.456 744-744/com.soyut.su.epos01 I/System.out: ttt useArray 8 [9.0, 8.3, 9.3, 8.0]

as it is printed it doesnt make any sense at all. Correct order should be : 8,8.3,9,9.3

来源:https://stackoverflow.com/questions/59919088/why-arraylist-doesnt-return-insertion-order-in-kotlin

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!