Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/322.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java:创建具有输入/输出的分隔符方法_Java_Arrays_String_Methods_Delimiter - Fatal编程技术网

Java:创建具有输入/输出的分隔符方法

Java:创建具有输入/输出的分隔符方法,java,arrays,string,methods,delimiter,Java,Arrays,String,Methods,Delimiter,我正在做一个课堂上的学习题,基本上它读一个字符串和一个字符。字符是分隔符。然后,它将在字符串中搜索分隔符,并创建一个长度等于找到分隔符次数的数组。然后,它将每个字符或字符串分配到数组中它自己的位置并返回它 也许我想得太多了,但最重要的是不要依赖各种字符串方法,而是创建自己的方法。如何使此方法仅将读取的字符串/字符分配到数组中的一个位置,而不是全部,并阻止它添加不必要的输出?非常感谢您的帮助/建议 public static String[] explode(String s, char d){

我正在做一个课堂上的学习题,基本上它读一个字符串和一个字符。字符是分隔符。然后,它将在字符串中搜索分隔符,并创建一个长度等于找到分隔符次数的数组。然后,它将每个字符或字符串分配到数组中它自己的位置并返回它

也许我想得太多了,但最重要的是不要依赖各种字符串方法,而是创建自己的方法。如何使此方法仅将读取的字符串/字符分配到数组中的一个位置,而不是全部,并阻止它添加不必要的输出?非常感谢您的帮助/建议

public static String[] explode(String s, char d){
    String []c;
    int count = 1;
    //checks to see how many times the delimter appears in the string and creates an      array of corresponding size
    for(int i = 0; i < s.length(); i++){
        if(d == s.charAt(i))
            count ++;
    }
    c = new String [count];
    //used for checking to make sure the correct number of elements are found
    System.out.println(c.length);
    //goes through the the input string "s" and checks to see if the delimiter is found
    //when it is found it makes c[j] equal to what is found
    //once it has cycled through the length of "s" and filled each element for c, it returns the array
    for(int i = 0; i < s.length(); i++){
            for(int j = 0; j < c.length; j++){
                if(d == s.charAt(i))
                    c[j] += s.substring(i-1);
            }
    }
    //provides output for the array [c] just to verify what was found
    for(int y = 0; y < c.length; y++)
        System.out.println(c[y]);
    return c;
}
public static void main(String [] args){
    String test = "a,b,c,d";
    char key = ',';
    explode(test,key);
}


    ^The following will output:
    4
    nulla,b,c,db,c,dc,d
    nulla,b,c,db,c,dc,d
    nulla,b,c,db,c,dc,d
    nulla,b,c,db,c,dc,d

    I'm aiming for:
    4
    a
    b
    c
    d
公共静态字符串[]分解(字符串s,字符d){
字符串[]c;
整数计数=1;
//检查delimter在字符串中出现的次数,并创建相应大小的数组
对于(int i=0;i

谢谢你

也许你可以试试这样的东西:

public static void main(String[] args){
    explode("a,b,c,d", ',');
}

public static void explode(final String string, final char delimiter){
    int length = 1;
    for(final char c : string.toCharArray())
        if(delimiter == c)
            length++;
    if(length == 1)
        return;
    final String[] array = new String[length];
    int index, prev = 0, i = 0;
    while((index = string.indexOf(delimiter, prev)) > -1){
        array[i++] = string.substring(prev, index);
        prev = index+1;
    }
    array[i] = string.substring(prev);
    System.out.println(length);
    for(final String s : array)
        System.out.println(s);
}
public static void explode(final String string, final char delimiter){
    final List<String> list = new LinkedList<>();
    int index, prev = 0;
    while((index = string.indexOf(delimiter, prev)) > -1){
        list.add(string.substring(prev, index));
        prev = index+1;
    }
    list.add(string.substring(prev));
    System.out.println(list.size());
    list.forEach(System.out::println);
}
上述程序的输出为:

4

a

b

c

d

或者,如果您想使用
列表
(和Java 8),您可以通过如下操作删除几行:

public static void main(String[] args){
    explode("a,b,c,d", ',');
}

public static void explode(final String string, final char delimiter){
    int length = 1;
    for(final char c : string.toCharArray())
        if(delimiter == c)
            length++;
    if(length == 1)
        return;
    final String[] array = new String[length];
    int index, prev = 0, i = 0;
    while((index = string.indexOf(delimiter, prev)) > -1){
        array[i++] = string.substring(prev, index);
        prev = index+1;
    }
    array[i] = string.substring(prev);
    System.out.println(length);
    for(final String s : array)
        System.out.println(s);
}
public static void explode(final String string, final char delimiter){
    final List<String> list = new LinkedList<>();
    int index, prev = 0;
    while((index = string.indexOf(delimiter, prev)) > -1){
        list.add(string.substring(prev, index));
        prev = index+1;
    }
    list.add(string.substring(prev));
    System.out.println(list.size());
    list.forEach(System.out::println);
}
publicstaticvoid分解(最终字符串,最终字符分隔符){
最终列表=新的LinkedList();
整数指数,上一个=0;
while((index=string.indexOf(delimiter,prev))>-1){
添加(string.substring(prev,index));
上一指数=指数+1;
}
list.add(string.substring(prev));
System.out.println(list.size());
list.forEach(System.out::println);
}

您似乎没有什么特别的问题。您的一般方法是正确的,但对于一般代码审查来说则不然(请尝试codereview.stackexchange.com)。如果您有特定问题,请编辑您的问题以提问。(另外,如果您正在滚动自己的,但未被禁止使用集合,
List
可能比裸数组更容易处理。)我将尝试使用您建议的chrysalis重建。我很感谢你的建议和提醒。谢谢你,乔希,除了下面这句话,我什么都懂。数组[i]=string.substring(pref);如果索引在循环之外,它是如何递增的?当循环退出时,
i
等于
length-1