英文:
How do i make a static reference to a non-static method?
问题
import java.util.Iterator;
import java.util.ArrayList;
import java.util.Arrays;
class Main {
public static void main(String[] args) {
ArrayList<String> original = new ArrayList<>(Arrays.asList("afd", "asdf", "aavdd", "sajf", "adnf", "afd", "fjfn"));
String find = "afd";
String replaceWith = "asd";
System.out.println(replaceIt(original, find, replaceWith));
}
public ArrayList<String> replaceIt(ArrayList<String> original, String find, String replaceWith) {
ArrayList<String> newList = new ArrayList<String>();
for (int y = 0; y < original.size(); y++) {
if (!original.get(y).equals(find))
newList.add(original.get(y));
else
newList.add(replaceWith);
}
original = newList;
return original;
}
}
如何调用 replaceIt
方法?我很困惑,我需要使它打印出该函数的输出。我很困惑,请有人帮帮我。
英文:
import java.util.Iterator;
import java.util.ArrayList;
import java.util.Arrays;
class Main {
public static void main(String[] args) {
ArrayList<String> original = new ArrayList<>(Arrays.asList("afd", "asdf", "aavdd", "sajf", "adnf", "afd", "fjfn"));
String find = "afd";
String replaceWith = "asd";
System.out.println(replaceIt(original, find, replaceWith));
}
public ArrayList<String> replaceIt(ArrayList<String> original, String find, String replaceWith){
ArrayList<String> newList = new ArrayList<String>();
for(int y = 0; y<original.size(); i++){
if(!original.get(y).equals(find))
newList.set(y, original.get(y));
newList.set(y, replaceWith);
}
original = newList;
return original;
}
}
How do i call the replaceIt method? I'm confused and I need to make it so it prints the output of that function. I'm so confused somebody please help.
答案1
得分: 0
* `public ArrayList<String> replaceIt()` 是一个实例方法,仅从 `Main` 实例/对象中调用。
* `public static void main` 是 `Main` 类的静态方法,静态方法无法直接访问实例方法和实例变量。
因此,要从静态 main 方法调用 `replaceIt()` 方法,请将 `replaceIt()` 声明为静态方法。
public static ArrayList<String> replaceIt(/*参数*/){
// 你的代码在这里
}
英文:
public ArrayList<String> replaceIt()
is an instance method, it is only called fromMain
instances/objects.public static void main
is a static method of classMain
, static methods can’t access instance methods and instance variables directly.
Therefor, to call replaceIt()
method from static main method, make replaceIt()
static.
public static ArrayList<String> replaceIt(/*arguments*/){
//your code goes here
}
答案2
得分: 0
在Main
中使用非静态方法有点奇怪。这并不像你要构造Main
的新实例一样(比如,你构造Person
类的新实例的方式)。
这种类型的通用辅助/工具函数通常放在名为Utils.java
或Helpers.java
的类中,并且在这些文件中声明为静态方法。这样你就可以调用:Utils.replaceIt(original, find, replaceWith));
。
英文:
Having non-static method in Main
is a little odd. It's not like you are going to construct new instances of Main (say, the same way you would construct new instances of a Person
class).
These type of generic helpers/utils functions usually go into some sort Utils.java
or Helpers.java
class, and are declared as static in these files. This way you would be able to invoke: Utils.replaceIt(original, find, replaceWith));
答案3
得分: 0
因为replaceIt()是一个实例方法,您只需要创建Main类的一个实例来调用它 -
public static void main(String[] args) {
...
Main m = new Main();
System.out.println(m.replaceIt(original, find, replaceWith));
}
英文:
Because replaceIt() is an instance method, you simply need to create an instance of the Main class to call it -
public static void main(String[] args) {
...
Main m = new Main();
System.out.println(m.replaceIt(original, find, replaceWith));
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论