简体   繁体   English

如何从给定的两个字符串数组形成第三个java数组

[英]How to form a Third java Array from given Two String Arrays

I am writing a java code for processing of signals where I have come accross a situation as explained below: 我正在编写用于处理遇到以下情况的信号的Java代码,如下所述:

INPUT GIVEN: String Arrays arr1 and arr2. 给定输入:字符串数组arr1和arr2。 OUTPUT REQD: String Array arr3 输出要求:字符串数组arr3

      String[] arr1= {"A", "c", "c",  "", "", "c", "c", "B", "", "c","c", "", "A",  "", "", "B", "B", "A"};
      String[] arr2= {"2", "3", "3",  "", "", "2", "1", "3", "", "2","3", "", "2",  "", "", "3", "2", "3"};
      String[] arr3= {"11", "",  "",  "", "", "",  "",  "8", "", "",  "", "", "2",  "", "", "3", "2", "3"};

ALGORITHM: 1. arr1 has elements of 4 types: "A", "B" and "c" and "". 算法:1. arr1具有4种类型的元素:“ A”,“ B”,“ c”和“”。

  1. arr2 has some Number Strings like "2", "3" etc. at corresponding index to "A", "B" and "c" in arr1, element "" in arr1 has corresponding element "" in arr2. arr2在arr1中与“ A”,“ B”和“ c”相对应的索引处具有一些数字字符串,如“ 2”,“ 3”等,arr1中的元素“”在arr2中具有相应的元素“”。

  2. arr3 is to be formed from arr1 and arr2. arr3由arr1和arr2组成。

  3. arr3 has Number Strings only corresponding to "A", "B" elemnts in arr1. arr3的数字字符串仅对应于arr1中的“ A”,“ B”元素。

  4. In arr3 the first Number String "11" is from the total of "2", "3", "3", "", "", "2", "1". 在arr3中,第一个数字字符串“ 11”来自“ 2”,“ 3”,“ 3”,“”,“”,“ 2”,“ 1”的总数。 These are the elements from "A" to "B"(including "A", excluding "B"). 这些是从“ A”到“ B”的元素(包括“ A”,不包括“ B”)。 "8" is from the total of "3", "", "2","3", "". “ 8”来自“ 3”,“”,“ 2”,“ 3”,“”的总数。 Next "2" is from the total of "2", "", "". 下一个“ 2”来自“ 2”,“”,“”的总数。 At last it is clear that "3", "2", "3" are from "3", "2", "3" respectively. 最后清楚的是,“ 3”,“ 2”,“ 3”分别来自“ 3”,“ 2”,“ 3”。

    Being new comer to programming and java I need help in the above case. 作为编程和Java的新手,在上述情况下我需要帮助。 Thanks in anticipation. 谢谢您的期待。

I suggest you to declare arr2 and arr3 as int arrays, that will help you do any mathematical operations easily. 我建议您将arr2和arr3声明为int数组,这将帮助您轻松进行任何数学运算。

And speaking about the approach for the solution, what we can do is maintain an index common for all 3 arrays and then perform the checks on arr1 and necessary operations on arr2 and arr3. 说到解决方案的方法,我们可以做的是维护所有3个数组的公共索引,然后对arr1进行检查,并对arr2和arr3进行必要的操作。

String[] arr1 = {"A", "c", "c",  "", "", "c", "c", "B", "", "c","c", "", "A",  "", "", "B", "B", "A"};
int[] arr2 = {2, 3, 3, 0, 0, 2, 1, 3, 0, 2, 3, 0, 2, 0, 0, 3, 2, 3};

int len = arr1.length, sum=0;
int[] arr3 = new int[len];

for(int i=0; i<len; i++){
    arr3[i] = 0;
}

for(int i = 0 ; i < len ; i++ ){
    sum=0;
    if(arr1[i].equals("A") || arr1[i].equals("B")){
        sum += arr2[i];
        int j=0;
        for(j = i+1; j < len && !arr1[j].equals("A") && !arr1[j].equals("B"); j++){
            sum += arr2[j];
        }
        arr3[i] = sum;
        i= j-1 ;
    }
}

The above piece of code now fills the arr3 with necessary values you can change them to strings by using the Integer.toString() method on each of the value. 上面的代码现在用必要的值填充arr3,您可以通过在每个值上使用Integer.toString()方法将它们更改为字符串。

If you need any further clarifications let me know. 如果您需要任何进一步的说明,请告诉我。 Happy coding ;) 快乐的编码;)

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

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