Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”.

Keep studying, keep studying!

Kotlin’s “It’s not written that way.”

Specific article links, follow a good study drops!

Juejin. Cn/post / 684490…

exercises

1. Create a Kotlin class that disallows external instance creation through the constructor and provides at least one instantiation method

If you use a singleton instead of a constructor, you can provide an instantiated version of yourself by providing a method that returns itself:

object A{
    fun getInstans():A{
        return this
    }
}
Copy the code

I also looked at other people’s answers.

class Sample private constructor(){
    companion object{
        fun newInstance(): Sample{
            return Sample()
        }
    }
}

fun main() {
    val sample = Sample.newInstance()
}
Copy the code

Another one:

class CreateInstances {

    fun callMethod() {
        val instance1 = getInstance()

        val instance2 = Second.getInstance()

        val instance3 = Third.getInstance()
    }

    companion object {
        fun getInstance(): CreateInstances {
            return CreateInstances()
        }
    }

    object Second {
        fun getInstance(): CreateInstances {
            return CreateInstances()
        }
    }

    class Third {
        companion object {
            fun getInstance() {

            }
        }
    }
}
Copy the code

If you want to create a class, you can use the object keyword. If you want to create a class, you can create a class. If you want to create an instance, you can use the constructor to create the instance.

2. Use Array, IntArray, and List respectively to “save the numbers from 1 to 100_000 and calculate the average of these numbers”, and print the execution time of these three data structures.

Write methods and call them in turn:

fun createArray() { val initArrayTime = System.currentTimeMillis() val array = Array(100000) { i -> i + 1 } val average = array.average() val endTime = System.currentTimeMillis() Log.d("MainActivity", "average = " + average + " array time = " + (endTime - initArrayTime)) } fun createIntArray() { val initIntArrayTime = System.currentTimeMillis() val intArray = IntArray(100000) { i -> i + 1 } val average = intArray.average() val endTime =  System.currentTimeMillis() Log.d("MainActivity", "average = " + average + " intArray time = " + (endTime - initIntArrayTime)) } fun createList() { val initListTime = System.currentTimeMillis() val list = List(100000) { i -> i + 1 } val average = list.average() val endTime = System.currentTimeMillis() Log.d("MainActivity", "average = " + average + " list time = " + (endTime - initListTime)) }Copy the code

In fact, the execution time may not be the same, but the execution time must be List > Array > IntArray.

Behind IntArray is an array of the basic data type int, fastest because there is no unboxing

Behind List is ArrayList

Behind Array is an Array of type Interger

If you’re using an array, if you know the type, it’s better to just use a special primitive array class.


🌈 follow me ac~ ❤️

Public account: Ni K Ni K

Hey hey ~ have no more for a long time, want to affit supervise and urge next send oneself! ^_^