【问题标题】:Modify Arraylist Java, Scope issue修改 Arraylist Java,范围问题
【发布时间】:2017-04-14 22:00:56
【问题描述】:

我正在处理一个个人项目,当我遇到问题时,我会编写一个“简化”版本的代码。我正在将数据分配给 Arraylist,并且我想使用 indexOf("") 访问索引。 我已经两年没有使用 java 了,而且我很难重新使用它。

这里是主要的,请致电Plo.java

import plo.tre;

public class Plo
{
    public static void main(String[] args)
    {
        tre test = new tre();

        // my problem
        StockDownloader.nouvel();
    }
}

这是我的 tre.java 另一个类:

import java.util.ArrayList;

public class tre
{
    private ArrayList<String> open;

    public tre()
    {
        open = new ArrayList<String>();

        String s = "chaine1,chaine2, chaine a, chaine b, chaine c";
        String str[] = s.split(",");

        for (int i = 0; i < str.length; i++) {
            open.add(str[i]);
        }
    }

    public void nouvel()
    {
        System.out.println(open.get(0));
        int test = open.indexOf(" chaine b");
    }
}

我想要做的是:1- 在 main 中调用 nouvel 方法,2- 可以访问在同一类中创建的 Arraylist,但填写不同的方法。

我知道这看起来很愚蠢,但我找不到解决方案。

非常感谢您的帮助。

【问题讨论】:

    标签: java arraylist scope call indexof


    【解决方案1】:

    在你的main()方法中,你需要使用tre类的引用(即test)来调用nouvel()方法,如下所示:

    test.nouvel();
    

    你能解释一下是什么问题吗?

    您正在使用类名StockDownloader(不确定它的定义位置)来调用方法nouvel()。类名只能用于调用类的static 方法/成员。要调用非static 方法,您需要使用对象引用(例如在您的代码中,test 被称为您创建的新tre 类对象的对象引用)。​​

    所以,问题是您需要使用对象引用来调用Tre 类的类成员,而您没有这样做。所以使用test(也就是对象的引用)来调用nouvel()方法。


    另外,我强烈建议您按照 Java 命名标准将类重命名为 Tre(或更有意义),即类名应以大写开头,以便代码可读。

    还有一点是,类构造函数是用来初始化实例变量的,你的代码很混乱,所以你可以重构它,如下所示:

    三级:

    public class Tre {
        private ArrayList<String> open;
    
        public Tre() {
            open = new ArrayList<String>();//just initialize ArrayList
        }
    
        public void loadData() {//add a new method to load ArrayList
            String s = "chaine1,chaine2, chaine a, chaine b, chaine c";
            String str[] = s.split(",");
    
            for (int i = 0; i < str.length; i++) {
                open.add(str[i]);
            }
        }
    
        public void nouvel() {
            System.out.println(open.get(0));
            int test = open.indexOf(" chaine b");
        }
    }
    

    Plo 类:

    public class Plo {
        public static void main(String[] args) {
            Tre test = new Tre();//create object for Tre with ref variable as test
            test.loadData();//call loadData() method using test ref.
            test.nouvel();//call nouvel() method using test ref.
        }
    }
    

    【讨论】:

    • 您好,谢谢您的回答,这确实使它起作用了。你能解释一下是什么问题吗?你是对的,我为没有使用命名标准而感到羞耻,我知道它们......下次我会发布它会更干净。
    • 加了上面的解释,看看
    • 知道了,我会使用你写的代码;我真的需要更新我的 java 基础。非常感谢您向我解释解决方案,非常感谢。
    【解决方案2】:

    试试tre.nouvel();,而不是StockDownloader.nouvel();

    【讨论】:

    • 嗨,感谢您的快速回答,这给了我这个错误:“错误:无法从静态上下文 tre.nouvel() 引用非静态方法 nouvel();”
    • 是的,我有点快。如上所述,test.nouvel() 本来是正确的做法。 :)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-09
    • 2020-11-30
    • 2012-12-11
    • 2010-10-11
    相关资源
    最近更新 更多