packageInfomation;publicclassData{publicString[]username=newString[20];publicString[]password=newString[20];publicvoiduserInfo(){username[0]="username";password[0]="passw...
package Infomation;
public class Data {
public String[] username = new String[20];
public String[] password = new String[20];
public void userInfo(){
username[0] = "username";
password[0] = "password";
}
}
package Program;
import Infomation.Data;
public class SC_Control {
private void deleteUser() {
data.userInfo();
System.out.println("请输入需要删除的用户名:");
String s1 = s.next();
System.out.println(printArray(data.username, s1));
if (printArray(data.username, s1) != -1) {
data.username[printArray(data.username, s1)] = null;
data.password[printArray(data.username, s1)] = null;
System.out.println("删除成功");
} else {
System.out.println("删除失败");
}
}
public int printArray(String[] array, String value) {
data.userInfo();
for (int i = 0; i < array.length; i++) {
if (array[i] == value) {
return i;
}
}
return -1;
}
代码如上,无论如何都返回值-1。
此外在用Arrays.asList(data.username).contains(username)时可以正常获取数组内容。
另外这种删除方法可行吗?删除后将每个元素提前。
展开