【发布时间】:2019-02-20 23:44:27
【问题描述】:
我通常不使用 Java,我目前正在尝试帮助朋友完成 Java 作业,这让我陷入困境
我正在尝试访问我在 Object 的构造函数中创建的数组,但我不知道如何访问它。
public class ADTbag {
String item = "Testing";
public ADTbag(int size) {
// This constructor has one parameter, name.
String[] bag = new String[size];
bag[0] = Integer.toString(size);
System.out.println("A bag was created with the size of " + size + " | " + bag[0]);
}
public void insert() {
/* Insert an item */
/* One Problem this public void doesn't have access to the bag var"
System.out.println(bag);
}
我觉得这是 java 中的一个简单概念,但我在谷歌上找不到任何对我有帮助的东西。我希望能够使用 insert 方法在包或字符串数组对象中插入一些东西。所以像这样。
public static void main(String []args) {
/* Object creation */
ADTbag myBag = new ADTbag(5);
String value = "Some Value";
/* I want to do this */
mybag.insert(value);
}
}
【问题讨论】:
-
你和你的朋友需要了解class和local变量的区别。
标签: java arrays object constructor