开发者

Appending a string to an array

I have to write a function that prepends if boolean is true or append if boolean is false a string to an array. I'm not exactly sure 开发者_开发知识库what this means? Do I just add a string to the first element of the array if I'm prepending or add a string to the last element of the array if I'm appending? Or what?


That could either mean append the String to the last String of the array or add a new element in your array.

But I really think that's about adding elements in an array (either at the start or at the end of the array).


Well the question states that append string to array depending on Boolean. Convert string to array and add each character to the Array. Appending to easy and for pre-pending you need to write a array routine to move the array elements to provide space for pre-pending string characters.


You could user StringBuilder instead of using array and just append ur string at the first of the stringBulder . E.g: StringBuilder str = new StringBuilder(); str.append("everyBody"); str.insert(0, "hello "); System.out.println(str.toString());

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜