简体   繁体   中英

How to get data from a parameterized constructor?

So i have String[] data that is returned from the parameterized constructor stringTokenizer(String str)

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

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.

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");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM