1

I am writing a code for a Signals Software. The summary of logic goes as: users input --> String--> split at white spaces--> ArrayPerWord --> SignalArrayPerArrayPerWord. Output is loop. Up to here my code works well. Only the remaining part left is how to join this loop-output in a sequence. I am searching for concatenating these arrays like String concatenation as:

str=str+str1;

something like:

SignalArrayPerArrayPerWord=SignalArrayPerArrayPerWord   + SignalArrayPerArrayPerWord1;

in other words:

array=array+array1;

I need help at this last step.

Oleg Estekhin
  • 8,063
  • 5
  • 49
  • 52
Garry
  • 107
  • 2
  • 7

6 Answers6

2

You can use addAll method with List<Integer>. Look at following example.

Integer[] arr={1,2,3};
Integer[] arr2={4,5,9};

List<Integer> res=new ArrayList<>();

res.addAll(Arrays.asList(arr));
res.addAll(Arrays.asList(arr2));

System.out.println(res);
Masudul
  • 21,823
  • 5
  • 43
  • 58
1

you can inspire from the following byte array concatanetion example.

   public static byte[] concatByteArrays(byte[]... arrays) {
      // Determine the length of the result array
      int totalLength = 0;
      for (int i = 0; i < arrays.length; i++) {
         totalLength += arrays[i].length;
      }

      // create the result array
      byte[] result = new byte[totalLength];

      // copy the source arrays into the result array
      int currentIndex = 0;
      for (int i = 0; i < arrays.length; i++) {
         System.arraycopy(arrays[i], 0, result, currentIndex, arrays[i].length);
         currentIndex += arrays[i].length;
      }

      return result;
   }
Ahmet Karakaya
  • 9,899
  • 23
  • 86
  • 141
1
ArrayUtils.addAll(array1, array2, ...)
Oleg Gryb
  • 5,122
  • 1
  • 28
  • 40
1

The following will concatenate arrays of same types:

String[] both = ArrayUtils.addAll(first, second);

Its similar to something like this:

ArrayUtils.addAll(["a", "b", "c"], ["1", "2", "3"]) = ["a", "b", "c", "1", "2", "3"]

Further details: doc link

There are multiple ways of concatenating java arrays efficiently, check this stackoverflow post too to find the best example:

stackoverflow post link

Community
  • 1
  • 1
fscore
  • 2,567
  • 7
  • 40
  • 74
  • Can I get the result as :{"a", "b", "c", "1", "2", "3"} or I'll have to get it manually? – Garry May 26 '14 at 16:11
  • If you store the result in a array and print that then you will get what you are looking for. – fscore May 26 '14 at 16:29
1

You can use List addAll Method..

List<String> signalArrayPerArrayPerWord = new ArrayList<String>();
List<String> signalArrayPerArrayPerWord1 = new ArrayList<String>();

List<String> concatArray = new ArrayList<String>();

//concat
concatArray.addAll(signalArrayPerArrayPerWord);
concatArray.addAll(signalArrayPerArrayPerWord1);

Hope that helps

Aviad
  • 1,539
  • 1
  • 9
  • 24
0

Use StringBuilder sb = new StringBuilder();

and in loop use sb.append( "loop element at index" );