1
int currentKeySize=8;

String message = new String();
message="This is a test message";   
String[] splitMessage = new String[currentKeySize];
Arrays.fill(splitMessage,"");

for (int i = 0; i < message.length(); ++i) {            
    splitMessage[i % splitMessage.length] += message.charAt(i);         
}
for (int i = 0; i < splitMessage.length; ++i) { 
    System.out.println(splitMessage[i]);
}

This code takes a message and splits it into an array of size 8, one character at the time so char(0) goes to String(0)...char(7) to String(7) and char(8) back to String(0).

I have whole program working around this concept and it is great...however now I have to dynamically modify currentKeySize,preferably using 'for' loop so it goes from 1 or 2 (number of splits) to n (lets say 20 splits).

I am aware of ArrayList< String >, however I did not have much luck with it as it is pretty new concept to me.I cannot figure out how to make it work in similar fashion to what I have here with appending characters.

Could someone demonstrate correct use of ArrayList< String >(did not accept without spaces between <>) to perform the same way as what is posted above?

Any help will be appreciated!

SOLVED. Thanks to micneer for exactly what I asked for =).

4 Answers 4

0

You can try like this, wish help you.

    int currentKeySize = 8;

    String message = new String();
    message="This is a test message";   
    List<String> list = new ArrayList<String>();

    for (int i = 0; i < currentKeySize; ++i) {
        list.add(i % currentKeySize, "");
    }
    for (int i = 0; i < message.length(); ++i) {      
        list.set(i % currentKeySize, list.get(i % currentKeySize) + message.charAt(i));
    }
    for (int i = 0; i < list.size(); ++i) { 
        System.out.println(list.get(i));
    }
1
  • Thank you. Exactly what i was missing...'list.set'. Commented Dec 25, 2014 at 7:00
0

You can simply add your split string into ArrayList:

Initialize list as following:

List<String> list=new ArrayList<String>();

And later on add to it as follwing:

list.add(spiltString);

The ArrayList class extends AbstractList and implements the List interface. ArrayList supports dynamic arrays that can grow as needed.

Standard Java arrays are of a fixed length. After arrays are created, they cannot grow or shrink, which means that you must know in advance how many elements an array will hold.

Array lists are created with an initial size. When this size is exceeded, the collection is automatically enlarged. When objects are removed, the array may be shrunk.

5
  • would you mind expanding on your answer? as i mentioned I am a bit new to Lists and well java for that matter and I do not yet see how this one line helps me or answers my question. Commented Dec 25, 2014 at 6:23
  • Do you require String[] to List<String> conversion? Commented Dec 25, 2014 at 6:27
  • I require to split the String into N parts. I dont believe I require conversion - What I require is substition of String[] into List or ArrayList<string>. i have a working code with this part that splits the message into 8 strings. But since I try to make the 'split number' dynamic I cannot use String[n] as it crashes. I need the above code to repeat from 2-20 size of arrays. And what you posted sadly does not give me any clearer understanding of how I could achieve that and assumes I still use String[] which I cannot use. Commented Dec 25, 2014 at 6:32
  • I knew I could do that. The thing I am struggling with is appending characters to the Strings once they are Inside the List just like in the example code I posted. In the example I posted I use currentKeySize=8 but this value will change. So I need something of sorts list.add(i % currentKeySize,"message here") but that message has to be appended character by character. And this is why I asked for example that would to perform the same way as what is posted above?. If however I am missing solution from your post. I am sorry - must be lack of sleep. Sorry for taking so much of your time. Commented Dec 25, 2014 at 6:53
  • Use StringBuilder and keep appending to it and add it to a index pf array. Commented Dec 25, 2014 at 7:02
0

You can do this.So you do not have to worry about initial size.

  String message = new String("This is a test message");
  String[] splitMessage =message.split("");
0
public static void main(String[] args) {
    int currentKeySize=8;

    String EMPTY_STRING = "";
    String message = new String();
    message="This is a test message";   
    List<String> splitMessage = new ArrayList<String>();
    Collections.fill(splitMessage, EMPTY_STRING);
    for (int i = 0; i < message.length(); ++i) {            
        try {
            String str = splitMessage.get(i%currentKeySize) + message.charAt(i); 
            splitMessage.set(i%currentKeySize, str);
        } catch(IndexOutOfBoundsException e) {
            //Adding EMPTY_STRING will convert character to String.
            splitMessage.add(EMPTY_STRING+message.charAt(i));
        }
    }
    System.out.println(splitMessage);
}

I think this piece of code will solve your problem.

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.