Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/303.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
(Java)将元素从字符串数组添加到字符串变量,直到特定索引?_Java_Arrays_String - Fatal编程技术网

(Java)将元素从字符串数组添加到字符串变量,直到特定索引?

(Java)将元素从字符串数组添加到字符串变量,直到特定索引?,java,arrays,string,Java,Arrays,String,这只是一个简化版本,但我想在不使用StringBuilder的情况下,将元素添加到字符串变量s的索引值count,有没有办法做到这一点?多谢各位 编辑:在不使用循环的情况下,是否可以使用字符串操作功能?不太明白您想要什么… 但我想你可以使用字符数组 public class sample { public static void main(String[] args) { String[] test = new String[1024]; int count = 0;

这只是一个简化版本,但我想在不使用StringBuilder的情况下,将元素添加到字符串变量s的索引值count,有没有办法做到这一点?多谢各位


编辑:在不使用循环的情况下,是否可以使用字符串操作功能?

不太明白您想要什么…
但我想你可以使用字符数组

public class sample {

public static void main(String[] args) {
    String[] test = new String[1024];
    int count = 0;
    test[count] = "33";
    count++;
    test[count] = "34";

    String s = new String();
char[]c=新字符[maxCount]

对于(inti=0;i很难说,你要的是什么

使用流可以轻松地连接连续数字:

char[] c = new char[maxCount] 
for(int i = 0;i<maxCount;i++){
  c[i] = "34";
}

一种方法是使用和:


使用您的测试数据生成的
3334

我们可以使用字符串的连接函数。 假设测试是字符串数组

String joinedString=String.join(“,Arrays.stream(test).limit(count).collect(collector.toList())
您可以使用String.join()

公共班机{

String s = String.join("", Arrays.copyOf(test, count + 1));
}


您想在
s
all
test[0..count]
值中追加值吗?您可以在循环中执行类似=>for(int i=0;i@ernest_k是的!意思是,我的字符串s将是“3334”!@Prakash13对不起,我忘了在不使用循环的情况下提及它!@Ken您可以创建一个类似每次递增count并插入test[count]的方法=某物,也做s+=测试[计数]或s+=某物
String s = String.join("", Arrays.copyOf(test, count + 1));
public static void main(String[] args) {
    String[] test = new String[1024];
    int count = 0;
    test[count] = "33";
    count++;
    test[count] = "34";

    String s = new String();
    System.out.println(s=String.join("", Arrays.copyOf(test, count + 1)));
}