Android-字节跳动算法题:给定ViewGroup打印其内所有的View

fun recursionPrint(root: View) {printView(root)if (root is ViewGroup) {for (childIndex in 0 until root.childCount) {val childView = root.getChildAt(childIndex)recursionPrint(childView)}}}递归确实可以很清晰的实现功能,但是它有一个致命的问题,当递归深度过深的时候,会爆栈。反应在程序上,就是会抛出 
摘要由CSDN通过智能技术生成

fun recursionPrint(root: View) {
printView(root)
if (root is ViewGroup) {
for (childIndex in 0 until root.childCount) {
val childView = root.getChildAt(childIndex)
recursionPrint(childView)
}
}
}

递归确实可以很清晰的实现功能,但是它有一个致命的问题,当递归深度过深的时候,会爆栈。反应在程序上,就是会抛出 StackOverflowError这个异常。

面试的时候,面试者解决问题的思路,使用了递归思想,通常都会很自然的问问 JVM 的栈帧,以及为什么会出现 StackOverflowError 异常。

当然这不是本文的重点,大家了解一下即可。

简单来说,每启动一个线程,JVM 都会为其分配一个 Java 栈,每调用一个方法,都会被封装成一个栈帧,进行压栈操作,当方法执行完成之后,又会执行弹栈操作。而每个栈帧中,当前调用的方法的一些局部变量、动态连接,以及返回地址等数据。

Java 栈和数据结构的栈结构一样,有两个操作,压栈(入栈)、弹栈(出栈),是一个先入后出(FILO)的结构。这一块的东西,延伸出来就比较多了,你可以简单的理解为调用方法就会压栈,方法执行完会弹栈。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
分析蓝牙为什么其他设备无法扫描到,打印如下”05-22 20:55:15.738 3861 3894 I ConfigStore: android::hardware::configstore::V1_0::ISurfaceFlingerConfigs::hasWideColorDisplay retrieved: 0 05-22 20:55:15.738 3861 3894 I ConfigStore: android::hardware::configstore::V1_0::ISurfaceFlingerConfigs::hasHDRDisplay retrieved: 0 05-22 20:55:15.738 3861 3894 I OpenGLRenderer: Initialized EGL, version 1.4 05-22 20:55:15.739 3861 3894 D OpenGLRenderer: Swap behavior 2 05-22 20:55:15.741 3861 3861 I ViewGroup: Drop empty frame. 05-22 20:55:15.743 3861 3861 I chatty : uid=1000(system) com.zhiying.bluetoothmodelservice identical 15 lines 05-22 20:55:15.743 3861 3861 I ViewGroup: Drop empty frame. 05-22 20:55:15.758 3861 3861 I bt.sink.bthelper: onReceive: android.bluetooth.adapter.action.STATE_CHANGED 05-22 20:55:15.758 3861 3861 I bt.sink.bthelper: BluetoothAdapter.ACTION_STATE_CHANGED 05-22 20:55:15.758 3861 3861 I bt.sink.bthelper: onReceive: android.bluetooth.adapter.action.STATE_CHANGED 05-22 20:55:15.759 3861 3861 I bt.sink.bthelper: BluetoothAdapter.ACTION_STATE_CHANGED 05-22 20:55:15.885 2131 2131 D HiRMS_RamManager: updateSceneType: msg = com.zhiying.bluetoothmodelservice:3861 scene = 1 05-22 20:55:15.889 2131 2131 D HiRMS_HwSysManager: doWritePidToCgroup pid:3861 cgroupPath:/sys/fs/cgroup/memory/topapp/cgroup.procs 05-22 20:55:15.919 3861 6797 D BluetoothA2dpSink: onBluetoothStateChange: up=false 05-22 20:55:15.920 3861 5703 D BluetoothA2dpSink: onBluetoothStateChange: up=false“
05-23

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值