Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/306.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,我们得到一个字符串数组,我们需要的是一个char[],即所有字符串中所有字符的数组 例如: 输入:[我,爱,你] 输出:[i,l,o,v,e,y,o,u] 首先,我制作了一个数组 然后我找到了所需char[]数组的长度 到目前为止,我已经尝试了以下方法: char[][] a1 = new char[str.length][]; for(int i =0;i<str.length;i++){ a1[i]=str[i].toCharArray(); } int total=0;

我们得到一个字符串数组,我们需要的是一个char[],即所有字符串中所有字符的数组 例如:

输入:[我,爱,你]

输出:[i,l,o,v,e,y,o,u]

首先,我制作了一个数组

然后我找到了所需char[]数组的长度

到目前为止,我已经尝试了以下方法:

char[][] a1 = new char[str.length][];

for(int i =0;i<str.length;i++){
    a1[i]=str[i].toCharArray();
}

int total=0;
for(int i =0;i<str.length;i++){
    total = total + a1[i].length;
}

char[] allchar = new char[total];

for(int i=0;i<str.length;i++){
    //NOW HERE I WANT TO MERGE ALL THE char[] ARRAYS TOGETHER.
//HOW SHOULD I DO THIS?
}
char[]]a1=新字符[str.length][];

对于(inti=0;i你可以这样做

char[] allchar = new char[total]; // Your code till here is proper

// Copying the contents of the 2d array to a new 1d array
int counter = 0; // Counter as the index of your allChar array
for (int i = 0; i < a1.length; i++) { 
    for (int j = 0; j < a1[i].length; j++) { // nested for loop - typical 2d array format
        allchar[counter++] = a1[i][j]; // copying it to the new array
    }
}
char[]allchar=new char[total];//在这里输入正确的代码
//将二维数组的内容复制到新的一维数组
int counter=0;//计数器作为allChar数组的索引
对于(inti=0;i
您可以执行以下方法

    public void converter(String[] stringArray) {


    char[] charsInArray = new char[500];    //set size of char array    
    int counterChars = 0;

    for (int i = 0; i < stringArray.length; i++) { 

        int counterLetters = 0; //declare counter for for amount of letters in each string in the array 

        for (int j = 0; j < stringArray[i].length(); j++) { 


            // below pretty self explanatory extracting individual strings and a
            charsInArray[counterChars] = stringArray[i].charAt(counterLetters);

            counterLetters++;
            counterChars++;
        }
    }


    }
public void转换器(String[]stringArray){
char[]charsInArray=new char[500];//设置char数组的大小
int counterChars=0;
对于(int i=0;i
String[]s=新字符串[]{“我”、“爱”、“你”};
整数长度=0;
对于(int i=0;i
公共静态void main(字符串[]args)
{
字符串句子=“Gokul Krishna是班长”;
字符串[]数组=句子。拆分(\\s”);
整数计数器;
//字符串字=新字符串();
for(字符串字:数组)
{
计数器=0;
char[]wordArray=word.toCharArray();

对于Java8中的(inti=0;i,我们可以使用流

public char[] convert(String[] words) {
    return Arrays.stream(words)
            .collect(Collectors.joining())
            .toCharArray();
}
在这里,我们使用 其中T是数组元素的类型。
而不是使用获取字符串。最后,我们使用字符串的方法获取字符数组。

停止头脑风暴,编写一些代码。这会有所帮助。
string.tocharray();
现在停止思考和编码。这将是一个好的开始。为什么这么粗鲁?初学者可以问StackOverflow的问题,不是吗?厌倦了我的所有选票!你可以问一个问题,但你需要表明你已经做了一些努力。
我怎么能…
是一个糟糕的格式,用于堆栈溢出问题。StackOverflow问题应该通常采用以下格式:
这是我想要完成的。这是我尝试过的。以下是我实际编译(或试图编译)的代码。我有这个结果。我期待这个结果。我做错了什么?
如果你想让其他人编写代码,将
字符串的数组变成
字符的数组,那么你应该雇佣一名程序员。最后,谢谢你,R.J!stringbuilder对于更长的输入会更有效。“仅限代码”答案很少帮助初学者理解。请考虑解释你的解决方案。
String[] sArray = {"i", "love", "you"};
    String s = "";
    for (String n:sArray)
        s+= n;
    char[] c = s.toCharArray();
public static void main(String[] args)
{
    String sentence="Gokul Krishna is class leader";
    String[] array=sentence.split("\\s");
    int counter;
    //String word = new String();
    for(String word:array)
    {
        counter=0;
        char[] wordArray=word.toCharArray();
        for(int i=0;i<wordArray.length;i++)
        {
            counter++;
        }
        System.out.println(word+ " :" +counter);
    }
}
public char[] convert(String[] words) {
    return Arrays.stream(words)
            .collect(Collectors.joining())
            .toCharArray();
}