简体   繁体   English

如何从参数化构造函数获取数据?

[英]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.

相关问题 如何从默认构造函数调用参数化构造函数? - how to call parameterized constructor from default constructor? 如何从另一个类中的public类型的参数化构造函数中调用默认类型的参数化构造函数? - How can I call parameterized constructor of default type from a parameterized constructor of type public which is in another class? 如何从java中的另一个类调用参数化构造函数 - How to call parameterized constructor from another class in java 如何在参数化构造函数中使用枚举? - How to use enum in a parameterized constructor? 在构造函数中使用参数化类型从名称实例化类 - Instantiating class from name with parameterized types in constructor 如何在使用 class 参数化的 java 中创建构造函数? - How to create constructor in java parameterized with class? 如何反序列化具有参数化构造函数的对象 - how to deserialize an object having a parameterized constructor 如何仅使用参数化构造函数为类创建对象? - how to create object for a class with only parameterized constructor? 如何从参数化类型方法参数中获取参数化类型类? - How can I get the parameterized-type class from a parameterized-type method argument? 从参数化构造函数调用默认构造函数的任何方法? - Any way to call the default constructor from a parameterized constructor?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM