gpt4 book ai didi

java - Android Java 代码中的这个语法是什么意思?

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:31:42 24 4
gpt4 key购买 nike

任何人都可以向我解释以下代码吗?是from the Android source code .

第一行在我看来是初始化一个整数数组,但是那些用大括号括起来的代码呢?我的意思是自大括号部分以来这些代码语法是否正确好像很纠结?

    // high priority first
mPriorityList = new int[mNetworksDefined];
{
int insertionPoint = mNetworksDefined-1;
int currentLowest = 0;
int nextLowest = 0;
while (insertionPoint > -1) {
for (NetworkAttributes na : mNetAttributes) {
if (na == null) continue;
if (na.mPriority < currentLowest) continue;
if (na.mPriority > currentLowest) {
if (na.mPriority < nextLowest || nextLowest == 0) {
nextLowest = na.mPriority;
}
continue;
}
mPriorityList[insertionPoint--] = na.mType;
}
currentLowest = nextLowest;
nextLowest = 0;
}
}

最佳答案

是的,那些代码块绝对没问题。它们是可行的语法。相反,它们很有用。

发生了什么,代码只是转移到一个未命名的 block ,为他们提供一个 block 范围。因此,在该 block 内定义的任何变量在外部都不可见。

int[] a = new int[10];
{
int x = 10;
a[0] = x;
System.out.println(x);
}

System.out.println(x); // Error. x not visible here.

所以,那些 大括号 只是创建了一个 local block scope,仅此而已。他们没有什么特别的。不过,您不会感受到上面代码中那些 block 的魔力。

这种编码方式通常用于最小化局部变量的范围,这绝对是个好主意,特别是当变量 code> 在该 block 内创建的不会在其他任何地方使用。

因此,如果在没有这些大括号的情况下使用,这些变量将只是挂起,等待垃圾收集器释放它们,在同时,享受 tripcurrent scope 的尽头,这可能是一个很长的 method

关于java - Android Java 代码中的这个语法是什么意思?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13122640/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com