2

Suppose I have a string array

NumText[]={“00111”, “01110”, “10110”}, 

now I want a new array

BinaryText[]={"0","0","1","1","1"......"1","1","0"}, 

which means I have to split each element of the array NumText[], and combine all the bits I get into a new array. What I can figure out is that I define a string array for each element, like

Binary0=NumText[0].split("");
Binary1=NumText[1].split("");
Binary2=NumText[2].split("");

After that I have to remove the leading zero of each BinaryX, and concatenate them together, which is really a bad idea. Any better ways? I appreciate your help.

4
  • You can try using Arrays.copyOfRange() Commented Mar 17, 2013 at 8:06
  • Why dont you convert these values to integer before you do the first split operation so that you dont have to remove the leading zeroes. Commented Mar 17, 2013 at 8:07
  • Use a nested loop. The outer should loop on NumText, and the inner should loop on the characters of each string. Commented Mar 17, 2013 at 8:08
  • @Eyal Schneider Nice solution, I've tried it. Commented Mar 17, 2013 at 8:28

4 Answers 4

1

use two loops the outer for the whole array and the inner for the string at each index:

StringBuilder temp = new StringBuilder();
for(int i=0; i < numText.length; i++){
   for( int j = 0; j < numText[i].length(); j++){
        temp.append(numText[i].charAt(j));
   }

}

char[] newArray = temp.toCharArray();
Sign up to request clarification or add additional context in comments.

1 Comment

It seems that the requested output is a string array, not a char array.
1

You can try this too...

String[] numText = { "00111", "01110", "10110" };
char[] binaryText = (Arrays.toString(numText).replace("[", "").replace("]", "").replaceAll("[, ]", "")).toCharArray();

Comments

0

I don't know why you removing the Zeros but here is my answer

String [] s = {"01 110"," 10010","01010"};
        ArrayList<String> list= new ArrayList<String>();
        for(int i =0 ; i<s.length;i++){
            s[i]=s[i].replaceAll("[0\\s]", "");
                for(int j = 0 ; j<s[i].length();j++){
                    list.add(Character.toString(s[i].charAt(j)));
                }
        }
        System.out.println(list.toString());

1 Comment

ArrayList used, that's cool. And what I wanted to move are not those 0s, but the 0 generated by 'split'. Since I split on an empty string, I guess the resulted array would have a leading 0. That is the case with my last effort to split an user input string letter by letter and store them in an array. Anyway, I won't have such problems with your solution.
0
String numText[] = { "00111", "01110", "10110" };
StringBuilder sb = new StringBuilder();

for (int i = 0; i < numText.length; i++) {
    numText[i] = numText[i].replaceAll("^[0]*", ""); //Remove leading zeros
    for (int j = 0; j < numText[i].length(); j++) {
        sb.append(numText[i].charAt(j));
    }
}
String[] output = sb.toString().split(""); 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.