例如,我需要将字符串“Example”按字符拆分并填充具有确定大小的字符数组,然后在数组已满时停止。
因此,如果字符数组有 10 个元素,则结果数组将包含组件:
{E,x,a,m,p,l,e,E,x,a}
只需将字符放入循环中的数组即可。您可以使用原始字符串的长度使用charAt(i % a.length())
.
String a = "Example";
char[] b = new char[10];
for (int i = 0; i < b.length; i++) {
b[i] = a.charAt(i % a.length());
}
for (char x : b) System.out.println(x);
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句