I want to create a split class for String.

Asked 1 months ago, Updated 1 months ago, 5 views

java.lang.IndexOutOfBoundsException, I understand what this error means, but I don't understand why b is output 3 times and a is output 2 times.
Please let me know

public class KString extensions Object {


    ArrayList<Character>kList=new ArrayList<Character>();

    public KString(char[] c) {
        kList.clear();
        for (inti=0;i<c.length;i++) {
            kList.add(i,c[i]);
        }
    }
    
    
    
    public KString[]split(charc){
        
        char split=c;
        ArrayList<Integer>array=new ArrayList<Integer>();
        
        for(inti=0,j=0;i<kList.size();i++){
            if(kList.get(i).equals(split)){
                array.add(j, i);
                j++;
            }
        }
        intk = 0;
        char[]vc = new char [array.get(0)];
        KString []ks = new KString [array.size()];
        for(inti=0;i<kList.size();i++){
            if(kList.get(i).equals(split)){
                ks[k] = new KString(vc);
                if((i==array.get(k))){
                    System.out.println("a");
                    vc = new char [(array.get(k+1))-(array.get(k)+1));
                }
                k++;
            } else {
                if(k==0){
                    vc[i-k] = kList.get(i);
                    System.out.println("b");
                } else {
                    vc[((i-1)-(array.get(k-1))))] = kList.get(i);
                }
            }
        }
        returnks;
    }
}

public class Next {

    public static void main(String[]args) {
        char[]c = new char[]{'a', 'b', 'c', '', 'd', 'e', 'f', '', 'g', 'h', 'i'};
        KStringks = new KString(c);
        ks.split(', ');
    }
}

Results

b
B
B
a
a
Exception in thread "main" java.lang.IndexOutOfBoundsException—Index2 out of bounds for length2
    at java.base/jdk.internal.util.Preconditions.outOfBounds (Preconditions.java:64)
    at java.base/jdk.internal.util.Preconditions.outOfBoundsCheckIndex (Preconditions.java:70)
    at java.base/jdk.internal.util.Preconditions.checkIndex (Preconditions.java:248)
    at java.base/java.util.Objects.checkIndex (Objects.java:372)
    at java.base/java.util.ArrayList.get (ArrayList.java:459)
    at ABC.KawaiString.split (KawaiString.java:57)
    at ABC.Next.main (Next.java:8)

java string

2022-09-30 13:56

1 Answers

The first to third characters ('a', 'b', 'c') do not match split and because k=0 (does not pass k++)
b is printed three times.

The fourth (', ') matches, so a is printed.
(i==array.get(k) will definitely be true)

The 5th to 7th ('d', 'e', 'f') will enter else due to discrepancy. There is no output because k == 0.

The eighth (', ') matches, so a is printed, but
Subsequent array.get(k+1) will result in an exception

I added the log for clarity.
("c" corresponds to "3." above)

publicKString[]split(charc){
        
        char split=c;
        ArrayList<Integer>array=new ArrayList<Integer>();
        
        for(inti=0,j=0;i<kList.size();i++){
            if(kList.get(i).equals(split)){
                array.add(j, i);
                j++;
            }
        }
        intk = 0;
        char[]vc = new char [array.get(0)];
        KString []ks = new KString [array.size()];
        for(inti=0;i<kList.size();i++){
            System.out.println("Cur Char:"+kList.get(i).toString()); // add
            if(kList.get(i).equals(split)){
                ks[k] = new KString(vc);
                if((i==array.get(k))){
                    System.out.println("a");
                    vc = new char [(array.get(k+1))-(array.get(k)+1));
                }
                k++;
            } else {
                if(k==0){
                    vc[i-k] = kList.get(i);
                    System.out.println("b");
                } else {
                    vc[((i-1)-(array.get(k-1))))] = kList.get(i);
                    System.out.println("c"); // add
                }
            }
        }
        returnks;
    }

The results are as follows

Cur Char:a
B
Cur Char:b
B
Cur Char:c
B
Cur Char: ,
a
Cur Char:d
c
Cur Char:e
c
Cur Char:f
c
Cur Char: ,
a
Exception in thread "main" java.lang.IndexOutOfBoundsException—Index2 out of bounds for length2
    at java.base/jdk.internal.util.Preconditions.outOfBounds (Preconditions.java:64)
    at java.base/jdk.internal.util.Preconditions.outOfBoundsCheckIndex (Preconditions.java:70)
    at java.base/jdk.internal.util.Preconditions.checkIndex (Preconditions.java:248)
    at java.base/java.util.Objects.checkIndex (Objects.java:359)
    at java.base/java.util.ArrayList.get (ArrayList.java:427)
    at KString.split(prog.java:36)
    atprog.main(prog.java:58)


2022-09-30 13:56

If you have any answers or tips


© 2022 OneMinuteCode. All rights reserved.