Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.
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 =).

share|improve this question

4 Answers 4

up vote 0 down vote accepted

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));
    }
share|improve this answer
    
Thank you. Exactly what i was missing...'list.set'. –  iluzek Dec 25 '14 at 7:00

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.

share|improve this answer
    
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. –  iluzek Dec 25 '14 at 6:23
    
Do you require String[] to List<String> conversion? –  Darshan Lila Dec 25 '14 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. –  iluzek Dec 25 '14 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. –  iluzek Dec 25 '14 at 6:53
    
Use StringBuilder and keep appending to it and add it to a index pf array. –  Darshan Lila Dec 25 '14 at 7:02

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("");
share|improve this answer
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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.