简体   繁体   English

在Java中将String []数组转换为动态char []数组?

[英]Converting String[] array to dynamic char[] array in java?

I have following piece of code and in this code I want to convert the String array into dynamically generated char array based on the length of String array. 我有以下一段代码,在这段代码中,我想根据String数组的长度将String数组转换为动态生成的char数组。 Is my approach/technique is feasible or i have to choose some other datatype...actually i have to perform logic on each and every character of word thats why i am breaking the String array to char[] array.... 是我的方法/技术可行还是我必须选择其他一些数据类型...实际上我必须对单词的每个字符执行逻辑,这就是为什么我将String数组拆分为char []数组。

private String[] function(String[] words) {
    String[] names = words;

    char[] CArray= null;

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

    for (int i = 0; i < CArray.length ; i++){
        System.out.print(CArray[i]);
    }

    return names;           
}

Guys plz try to under stand that I want the names of char[] array to be generated in sequence quto based on the length of String array means there should be 4 char[] arrays if there are 4 words having name in a sequence such as S0,S1,S2,S3 or anything else..!!!!! 伙计们请尝试了解一下,我希望基于String数组的长度在序列quto中生成char []数组的名称,这意味着如果序列中有4个单词的名称(例如, S0,S1,S2,S3或其他任何东西.. !!!!!

Okay guys plz this is what I want to achieve 好的,伙计们,这就是我想要实现的目标
char[] S1 or anyname = words[0].toCharArray(); char [] S1或anyname = words [0] .toCharArray();
char[] S2 or anyname = words[1].toCharArray(); char [] S2或anyname = words [1] .toCharArray();
so on...depending upon the no of String arrays passed,,,such that I am left with char[] arrays S1,S2,S3 to carry my further operationss.....plz plz now figure out it ..!! 依此类推...取决于传递的String数组的编号,这样我就剩下char []数组S1,S2,S3来进行我的进一步操作。....plz plz现在找出它..! ! Hope i made clear understanding of the ques..?? 希望我对问题有清楚的了解。

What you want is to put every character within the string array into the char array????. 您想要的是将字符串数组中的每个字符放入char数组中。

If it is, you have several ways to do this. 如果是这样,您有几种方法可以做到这一点。

To avoid using an array list (increases its capacity dinamycally), you will have to know the amount of character inside each index of you string array, go through each index adding the amount of character to a variable that you previouly had declared (lets called sumChar), then create the char array with a capacity equals to sumChar char [] Carray=new chat[sumChar]. 为了避免使用数组列表(大幅度增加其容量),您将必须知道字符串数组的每个索引内的字符数,遍历每个索引,将字符数添加到先前声明的变量中(称为sumChar),然后创建容量等于sumChar char [] Carray = new chat [sumChar]的char数组。

then do something like this int indexPos=0; 然后做这样的事情int indexPos = 0;

for (int i=0;i<names.length;i++)
{
   for (int c=0;c<names[i];length;c++)
    {
          Carray[indexPos++]=names[i].charAt(c);
    }
}

that's all, the code above I made it without tested it , so any problem feel free to ask 就是这样,上面的代码我未经测试就完成了,所以任何问题都可以随时提出

I think this is what you are looking for: 我认为这是您要寻找的:

private static char[] stringArrayToCharArray (String[] words) {
    int arrayLength = 0;

    // Calculate the total size of the char array by adding the strings sizes
    for (final String word : words)
        arrayLength += word.length();

    // Create the char array
    char[] charArray = new char[arrayLength];

    int arrayIndex = 0;

    // Fill the array
    for (final String word : words)
        for (int i=0; i<word.length(); i++)
            charArray[arrayIndex++] = word.charAt(i);

    return charArray;           
}

Here is how to use it: 使用方法如下:

String[] strs = {"test", "other"};
char[] chars = stringArrayToCharArray(strs);

I am afraid this is not even close.. did you execute the code and see its results? 恐怕还没有结束..您是否执行了代码并查看了结果?

  1. Not to be nit picking, but this is sort of important: variable names in Java begin with lower case, so cArray instead of CArray. 别无所求,但这很重要:Java中的变量名以小写开头,因此使用cArray而不是CArray。 Saves readers a lot of confusion (is it a variable? Is it a class? etc). 避免使读者感到困惑(它是变量吗?是类吗?等等)。

  2. What's the purpose of this line? 这条线的目的是什么? Why not just use words? 为什么不只使用单词呢?

    String[] names = words; String []名称=单词;

  3. The code below results in CArray to be an array of characters comprising the LAST word. 下面的代码导致CArray为包含LAST字的字符数组。 All other words but the last one are discarded. 除最后一个以外的所有其他单词都将被丢弃。

for (int i = 0; i < names.length ; i++){ CArray = names[i].toCharArray();

Perhaps you meant to nest the second loop into the body of the first loop? 也许您打算将第二个循环嵌套到第一个循环的主体中?

  1. And then you are returning names , which is precisely the same as words , no char arrays there. 然后,您将返回names ,该nameswords完全相同,那里没有char数组。

  2. Perhaps you want function to return an array of arrays of chars? 也许您想要function返回一个字符数组的数组?

  3. I am not sure you really want to convert into an array of arrays of chars, as opposed to, say, using StringBuffer or regular expressions. 我不确定您是否真的要转换为chars数组的数组,而不是使用StringBuffer或正则表达式。 Most text manipulation in Java is done this way. Java中的大多数文本操作都是通过这种方式完成的。

I recommend studying more before plunging into coding.. 我建议您在学习编码之前先学习更多。

Here's a program then to demonstrate what you asked: 这是一个程序来演示您的要求:

public class CharArray {

public CharArray() {
    String[] words = { "First", "Second" };

    for (String word : words) {
        for (char character : word.toCharArray()) {
            System.out.print(character + " ");
        }
        System.out.println();
    }
}

public static void main(String[] args) {
    new CharArray();
}
}

The words present in the word array will come one by one to the character variable in the constructor. 单词数组中出现的单词将一一指向构造函数中的character变量。 Do whatever you want to do with them there. 在那里做任何您想做的事。

Hope it helps. 希望能帮助到你。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM