表达式的类型必须是一个数组类型,但它解析为ArrayList(试图比较两个数组中的字符串 [英] The type of the expression must be an array type but it resolved to ArrayList (trying to compare string in two arrays
问题描述
下面是整个代码:
我得到错误在for循环时,试图比较两个值使用.equals(不知道如果它的正确的方法,, ...我是新来的这个)请帮助!
<$ p $ (); $ {
} public class comparer {
public void compare(){
$ b $ ArrayList NameofFileinDir = new ArrayList
ArrayList Stocks = new ArrayList();
//填充阵列,以在DIR
尝试{
扫描器读取=新的扫描程序文件名(新文件( G:/Programming/StockDataDownload/NameOfFileinDir.txt));
字符串FileCode;
while(reads.hasNext()){
FileCode = reads.nextLine(); //读下一行
NameofFileinDir.add(FileCode);
}
reads.close();
}
catch(IOException e){
}
//用原始数据填充数组
try {
Scanner reads = new Scanner(new File G:/Programming/StockDataDownload/AllSecurities.txt));
字符串StockCode;
while(reads.hasNext()){
StockCode = reads.nextLine(); //读取下一行
Stocks.add(StockCode);
}
reads.close();
为(INT I = 0; I< Stocks.size();我++){
。对于(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 [一世]);
catch(IOException e){
}
}}
-
ArrayList
不是数组
。获得索引i的元素的语法是不同的:
list.get(i)
vsARR [I]
。阅读ArrayList
,特别是get(int)
方法。 研究什么
String.equals(Object)
返回,这意味着什么。观看 Object.equals(Object)
也不会受到伤害,因为默认情况下,java中的每个对象都会默认这个。 考虑使用foreach循环而不是旧的for循环。旧的for循环可以是疯狂的有用的,但这里没有意义。 (字符串文件名:NameofFileinDir)$ b
for(String stock:Stocks){
。凉。所以你有一个
{
if(stock.equals(filename)){
System.out.println(Stock:+ stocks);
} else {
System.out.println(Stock not in files:+ stocks);
$ / $ c $ $ $ $ $ $ $ $ $ $ c> ArrayList Stocks = new ArrayList();ArrayList
。什么?!现在,这在技术上不是非法的,但如果你的java编译器支持类型参数,你真的需要使用它们。用列出
。现在,每个人都能立即知道你的代码是什么。另外,编译器将保证你的列表只包含字符串。如果你试图做一些疯狂的或愚蠢的事情,比如把一些整数和映射到你的字符串中,编译器就会呕吐。 在if .. else是你的意思,你的逻辑是致命的缺陷。如果您正在比较的当前股票字符串和文件字符串不相等,则打印股票不在文件列表中。但从逻辑上讲,这是否意味着没有一个股票字符串和文件字符串是平等的?< String> stocks = new ArrayList< String>()
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!
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) {
}
}}
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 only contain 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?
这篇关于表达式的类型必须是一个数组类型,但它解析为ArrayList(试图比较两个数组中的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!