java - 将字符串值与布尔值进行比较
问题描述
我正在尝试将布尔值(所有这些都是0
)与这个字符串数组的值进行比较data[] = "1,0,0,0,0,0,0,0,0,0,0,0,1"
。这是我的代码:
recipeIngrediants = new boolean[numberOfCuisines][numberOfIngrediants];
int i = 0;
while(file.hasNextLine()){
String temp = file.nextLine();
String[] data = temp.split(",");
for(int j=0; j < recipeIngrediants.length; j++){
String c = data[j];
if(c == "1"){
recipeIngrediants[i][j] = true;
}
else{
recipeIngrediants[i][j] = false;
}
}
i++;
}
我收到一个错误,说它是类型不匹配。编辑:修复了类型不匹配错误,但对于布尔值中的所有值,它仍然给我一个 false 值
问题:
我还能如何比较这些值以使 2D 数组在具有 a的任何位置都recipeIngrediants
相等?true
data
1
解决方案
我认为您想为每个评估为true
的字符串分配数组项,否则:c
"1"
false
recipeIngrediants = new boolean[numberOfCuisines][numberOfIngrediants];
int i = 0;
while(file.hasNextLine()){
String temp = file.nextLine();
String[] data = temp.split(",");
for(int j=0; j < recipeIngrediants.length; j++){
String c = data[j];
recipeIngrediants[i][j] = (c != null) && (c.equals("1"));
}
i++;
}
推荐阅读
- android - 匹配父宽度属性在 recyclerView 中不起作用
- django - 我可以在 def 函数中使用 django 文件值吗?
- python - 从字典列表中删除重复键,仅保留值最大的键值
- python - 在 anaconda 虚拟环境和激活/停用/删除的使用之间切换
- passwords - Java 11 HttpClient - 缺少表单参数:grant_type
- java - 无法配置数据源:未使用 Spring Boot 指定“url”属性
- c# - 位运算符 AND & 位移
- flutter - 在 PWA 颤振项目中获取用户位置
- dotnetnuke - DNN 9:如何更改 TextEditor 标准行为?
- .net - 在 Dockerfile 中使用 Dotnet 和 MSBuild 工具