android - Lint 警告:变量已分配给此值
问题描述
Variable is already assigned to this value
执行以下操作时收到 lint 警告
String[] sa =getStringArray();
sa = modifyArrayandMakeAwesomer(sa); //get the warning here
对我来说似乎是一个新的警告。也许我的 lint 设置已经改变。代码按预期工作,没有任何错误。这是不好的做法吗?我应该声明第二个字符串数组吗?
解决方案
Because modifyArrayandMakeAwesomer(sa)
is modifying your data using its reference,
class Person {
String name;
}
// this method just return the string value after making it uppercase,
public static Person modifyReference(Person p)
{
p.name = "Gaurav";
return p; // we don't need to return this from here since we are modifying directly to the reference.
}
public static int main(String[] args)
{
Person p = new Person();
p.name = "Max";
System.out.println(p.name);
modifyReference(p); // this is what you should do,
p = modifyReference(p); // this is useless, since you have passed the reference of Person class
System.out.println(p.name);
}
推荐阅读
- ms-access - 使用表单将发票从 Access 导出到 QuickBooks 时遇到问题
- r - 为什么摘要在几百列之后不显示结果?
- python - 为什么我的代码在每次运行后从相同图像中的相同列表中查找更多对象?
- reactjs - TypeError:无法读取未定义的属性“图像” - React & Gatsby
- typescript - 引入两个别名的模拟导入
- flutter - 如何在颤动中对多个小部件使用相同的键?
- php - 如何在 WooCommerce 中获取用户的活动订单 ID 和订单状态
- reactjs - 如何使用参数调用 React Context 函数
- java - 如何将文件夹构建到 spring boot gradle 项目的 .jar 根目录中?
- javascript - 来自 Firebase 的未定义道具