表达式的类型必须是一个数组类型,但它解析为ArrayList(试图比较两个数组中的字符串 [英] The type of the expression must be an array type but it resolved to ArrayList (trying to compare string in two arrays

查看:1749
本文介绍了表达式的类型必须是一个数组类型,但它解析为ArrayList(试图比较两个数组中的字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Iam尝试比较数组中的每个字符串或int与另一个数组,然后根据字符串是否存在于另一个数组中打印结果:
下面是整个代码:
我得到错误在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){
}

}}


解决方案


  1. ArrayList 不是数组获得索引i的元素的语法是不同的: list.get(i) vs ARR [I] 。阅读 ArrayList ,特别是 get(int)方法。
  2. 研究什么 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编译器支持类型参数,你真的需要使用它们。用列出< String> stocks = new ArrayList< String>()。现在,每个人都能立即知道你的代码是什么。另外,编译器将保证你的列表只包含字符串。如果你试图做一些疯狂的或愚蠢的事情,比如把一些整数和映射到你的字符串中,编译器就会呕吐。 在if .. else是你的意思,你的逻辑是致命的缺陷。如果您正在比较的当前股票字符串和文件字符串不相等,则打印股票不在文件列表中。但从逻辑上讲,这是否意味着没有一个股票字符串和文件字符串是平等的?


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

}}

解决方案

  1. An ArrayList is not an array. The syntax to get the element at index i is different: list.get(i) vs arr[i]. Read up on ArrayList, especially the get(int) method.

  2. Research what String.equals(Object) returns and what that means. Glancing at Object.equals(Object) also wouldn't hurt, since every object in java gets this one by default.

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

  4. ArrayList Stocks = new ArrayList();. Cool. So you have an ArrayList. Of what?! Now, this isn't technically illegal, but if your java compiler supports type parameters, you really need to use them. Write instead List<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.

  5. 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆