修改 Arraylist Java,作用域问题
Modify Arraylist Java, Scope issue
我正在做一个个人项目,当我遇到麻烦时,我会编写它的 'simpify' 版本。我正在将数据分配给 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 另一个 class :
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- 主要调用 nouvel 方法,2- 可以访问在同一 class 中创建的 Arraylist,但填写不同的方法。
我知道这看起来很傻,但我找不到解决方案。
非常感谢您的帮助。
而不是 StockDownloader.nouvel();
,试试 tre.nouvel();
在你的main()
方法中,你需要使用tre
class(即test
)的引用来调用nouvel()
方法如下图:
test.nouvel();
Could you explain me what was the problem?
您正在使用 class 名称 StockDownloader
(不确定它在哪里定义)来调用方法 nouvel()
。 Class 名称只能用于调用 class 的 static
methods/members。要调用非 static
方法,您需要使用对象引用(就像在您的代码中 test
被称为您创建的新 tre
class 对象的对象引用).
所以,问题是您需要使用对象引用来调用 Tre
class 的 class 成员,而您没有这样做。所以使用test
(也就是对象的引用)来调用nouvel()
方法。
此外,我强烈建议您按照 Java 命名标准将 class 重命名为 Tre
(或更有意义),即 class 名称应以大写字母开头以便代码可读。
还有一点是 class 构造函数用于初始化实例变量,您的代码很混乱,因此您可以重构它,如下所示:
三 class:
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");
}
}
普洛class:
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.
}
}
我正在做一个个人项目,当我遇到麻烦时,我会编写它的 'simpify' 版本。我正在将数据分配给 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 另一个 class :
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- 主要调用 nouvel 方法,2- 可以访问在同一 class 中创建的 Arraylist,但填写不同的方法。
我知道这看起来很傻,但我找不到解决方案。
非常感谢您的帮助。
而不是 StockDownloader.nouvel();
,试试 tre.nouvel();
在你的main()
方法中,你需要使用tre
class(即test
)的引用来调用nouvel()
方法如下图:
test.nouvel();
Could you explain me what was the problem?
您正在使用 class 名称 StockDownloader
(不确定它在哪里定义)来调用方法 nouvel()
。 Class 名称只能用于调用 class 的 static
methods/members。要调用非 static
方法,您需要使用对象引用(就像在您的代码中 test
被称为您创建的新 tre
class 对象的对象引用).
所以,问题是您需要使用对象引用来调用 Tre
class 的 class 成员,而您没有这样做。所以使用test
(也就是对象的引用)来调用nouvel()
方法。
此外,我强烈建议您按照 Java 命名标准将 class 重命名为 Tre
(或更有意义),即 class 名称应以大写字母开头以便代码可读。
还有一点是 class 构造函数用于初始化实例变量,您的代码很混乱,因此您可以重构它,如下所示:
三 class:
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");
}
}
普洛class:
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.
}
}