首页 > 解决方案 > Lint 警告:变量已分配给此值

问题描述

Variable is already assigned to this value 执行以下操作时收到 lint 警告

String[] sa =getStringArray();
sa = modifyArrayandMakeAwesomer(sa);  //get the warning here

对我来说似乎是一个新的警告。也许我的 lint 设置已经改变。代码按预期工作,没有任何错误。这是不好的做法吗?我应该声明第二个字符串数组吗?

标签: androidandroid-studiowarningslint

解决方案


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);
}


推荐阅读