Java 表达式的类型必须是数组类型,但它解析为 ArrayList(尝试比较两个数组中的字符串
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18814182/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
The type of the expression must be an array type but it resolved to ArrayList (trying to compare string in two arrays
提问by Faisal Mehmood
Iam trying to compare each string or int in array with another array then print the results according to whether the string exists or not in the other array: Below is the whole code: I get error in the for loops when trying to comparing two values using .equals(not sure if its right method,,... I am new to this) Please help!
我正在尝试将数组中的每个字符串或 int 与另一个数组进行比较,然后根据字符串是否存在于另一个数组中来打印结果:以下是整个代码:尝试比较两个值时,我在 for 循环中遇到错误使用.equals(不确定它的方法是否正确,...我是新手)请帮忙!
public class comparer {
public void compare (){
ArrayList NameofFileinDir = new ArrayList ();
ArrayList Stocks = new ArrayList();
// populate array with files names in dir
try {
Scanner reads = new Scanner (new File("G:/Programming/StockDataDownload/NameOfFileinDir.txt"));
String FileCode;
while (reads.hasNext()){
FileCode = reads.nextLine(); //read next line
NameofFileinDir.add(FileCode);
}
reads.close();
}
catch (IOException e) {
}
// populate array with original stocks
try {
Scanner reads = new Scanner (new File("G:/Programming/StockDataDownload/AllSecurities.txt"));
String StockCode;
while (reads.hasNext()){
StockCode = reads.nextLine(); //read next line
Stocks.add(StockCode);
}
reads.close();
for(int i = 0; i < Stocks.size(); i++) {
for (int j = 0; j < NameofFileinDir.size(); j++) {
if (Stocks[i].equals(NameofFileinDir[j])) > 0) {
System.out.println("Stock:" + Stocks[i]);}
else{
System.out.println("Stock not in files:" + Stocks[i]);
}
}
}
}
catch (IOException e) {
}
}}
回答by Tom G
You can't use array notation on an ArrayList
. You need to call get(index)
instead.
您不能在ArrayList
. 你需要打电话get(index)
。
回答by Mangoose
change Stocks[i] to Stocks.get(i) and NameofFileinDir[j] to NameofFileinDir.get(j)
将 Stocks[i] 更改为 Stocks.get(i) 并将 NameofFileinDir[j] 更改为 NameofFileinDir.get(j)
Additional suggestions:
补充建议:
Also make NameofFileinDir to A hashSet for better performance
还将 NameofFileinDir 设为 A hashSet 以获得更好的性能
Also follow java camelCase naming convention like change NameofFileinDir to nameofFileinDir
还要遵循 java camelCase 命名约定,例如将 NameofFileinDir 更改为 nameofFileinDir
回答by Prabhaker A
You can't get ArrayListobjects by using []
operator like arrays.Use get()
method on ArrayList
to get the objects.
So change the code
您不能通过使用像 arrays.Use方法这样的运算符来获取对象来获取ArrayList对象。所以改代码 []
get()
ArrayList
if (Stocks[i].equals(NameofFileinDir[j])) > 0) {
to
到
if (Stocks[i].equals(NameofFileinDir.get([j]))) > 0) {
回答by Klicky
Your error is here:
你的错误在这里:
if (Stocks[i].equals(NameofFileinDir[j])) > 0) {
First, you should say Stocks.get(i).equals(NameofFileinDir.get(j)
The equals
function returns a boolean, so you compare wether true/false > 0
.
You can just drop the > 0)
首先,你应该说Stocks.get(i).equals(NameofFileinDir.get(j)
该equals
函数返回一个布尔值,因此你比较阉羊true/false > 0
。你可以放下> 0)
回答by Danny
If you're using ArrayList, then you need to use the get(index) method to access the variable at that index. Also the .equals(object) returns a boolean value so no need of > 0
如果您使用的是 ArrayList,那么您需要使用 get(index) 方法来访问该索引处的变量。此外 .equals(object) 返回一个布尔值,因此不需要 > 0
if (Stocks.get(i).equals(NameOfFileINDir.get(j)) {
}
回答by djeikyb
An
ArrayList
is not anarray
. The syntax toget the element at index i
is different:list.get(i)
vsarr[i]
. Read up onArrayList
, especially theget(int)
method.Research what
String.equals(Object)
returns and what that means. Glancing atObject.equals(Object)
also wouldn't hurt, since every object in java gets this one by default.Consider using a foreach loop instead of the old for loop. The old for loop can be crazy useful, but there's no point here. Look how much cleaner this is:
for (String stock : Stocks) { for (String filename : NameofFileinDir) { if (stock.equals(filename)) { System.out.println(" Stock: " + stocks); } else { System.out.println("Stock not in files: " + stocks); } } }
ArrayList Stocks = new ArrayList();
. Cool. So you have anArrayList
. Of what?! Now, this isn't technically illegal, but if your java compiler supports type parameters, you really need to use them. Write insteadList<String> stocks = new ArrayList<String>()
. Now everyone reading your code immediately knows what the list holds. Also, the compiler will guarantee that your list will onlycontain strings. If you try and do something insane or stupid, like throwing some Integers and Maps into the list with your strings, the compiler will puke.If what you print in the if..else is what you mean, your logic is fatally flawed. You print "stock not in list of files" if the current stock string and file string you happen to be comparing are not equal. But logically, does this mean none of the stock strings and file strings are equal?
An
ArrayList
不是array
。to 的语法get the element at index i
不同:list.get(i)
vsarr[i]
. 继续阅读ArrayList
,尤其是get(int)
方法。研究什么
String.equals(Object)
返回以及这意味着什么。看一眼Object.equals(Object)
也无妨,因为 Java 中的每个对象默认都会得到这个。考虑使用 foreach 循环而不是旧的 for 循环。旧的 for 循环可能非常有用,但这里没有意义。看看这有多干净:
for (String stock : Stocks) { for (String filename : NameofFileinDir) { if (stock.equals(filename)) { System.out.println(" Stock: " + stocks); } else { System.out.println("Stock not in files: " + stocks); } } }
ArrayList Stocks = new ArrayList();
. 凉爽的。所以你有一个ArrayList
. 什么?!现在,这在技术上并不违法,但是如果您的 Java 编译器支持类型参数,您就真的需要使用它们。改写List<String> stocks = new ArrayList<String>()
。现在,每个阅读您代码的人都会立即知道列表中包含的内容。此外,编译器将保证您的列表仅包含字符串。如果您尝试做一些疯狂或愚蠢的事情,例如将一些整数和映射与字符串一起放入列表中,编译器会呕吐。如果您在 if..else 中打印的是您的意思,那么您的逻辑存在致命缺陷。如果您碰巧比较的当前库存字符串和文件字符串不相等,则打印“库存不在文件列表中”。但从逻辑上讲,这是否意味着股票字符串和文件字符串都不相等?
回答by FAIZAN AHMED KHAN
If you dont want to use for each loop then you can follow this.
如果您不想为每个循环使用,那么您可以按照此操作。
for(int i = 0; i < Stocks.size(); i++) {
for (int j = 0; j < NameofFileinDir.size(); j++) {
if (Stocks[i].equals(NameofFileinDir[j])) > 0) {
System.out.println("Stock:" + Stocks[i]);}
else{
System.out.println("Stock not in files:" + Stocks[i]);
}
}
}
In the abovecode, instead of having Stocks[i], have this :
在上面的代码中,没有 Stocks[i],而是有这个:
for(int i = 0; i < Stocks.size(); i++) {
for (int j = 0; j < NameofFileinDir.size(); j++) {
if (Stocks.get(i).equals(NameofFileinDir.get(j))) > 0) {
System.out.println("Stock:" + Stocks[i]);}
else{
System.out.println("Stock not in files:" + Stocks.get(i));
}
}
}