[英]How to get data from a parameterized constructor?
So i have String[] data that is returned from the parameterized constructor stringTokenizer(String str) 所以我有从参数化构造函数stringTokenizer(String str)返回的String []数据
The question is how do you write the code in the method below so that the variable "tokens" can get the data from the constructor above 问题是如何在下面的方法中编写代码,以便变量“ tokens”可以从上面的构造函数获取数据
public String[] arr(String str){
String[] tokens = null;
tokens = str.split("\\s+");
return tokens;
}
public void print(){
String[] tokens = arr(); //How am i suppose to write this line so that i an get the data from the method above?
int size = tokens.length;
for(int i=0;i<size;i++){
System.out.print(tokens[i]);
}
}
The constructor would store the initialized tokens in a class variable. 构造函数会将初始化的令牌存储在类变量中。
From there, any class method (including arr()) can access that data. 从那里,任何类方法(包括arr())都可以访问该数据。
class StringTokenizer {
String str;
StringTokenizer(String str) {this.str=str;}
public String[] getTokens() {
String[] tokens;
tokens = this.str.split("\\s+");
return tokens;
}
}
方法arr()
有一个参数,因此在调用该方法时,需要提供String
,例如:
String[] tokens = arr("This is a string that needs to be tokenized");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.