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.

0

6 Answers 6

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);
Sign up to request clarification or add additional context in comments.

Comments

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

2 Comments

Can I get the result as :{"a", "b", "c", "1", "2", "3"} or I'll have to get it manually?
If you store the result in a array and print that then you will get what you are looking for.
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;
   }

Comments

1
ArrayUtils.addAll(array1, array2, ...)

Comments

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

Comments

0

Use StringBuilder sb = new StringBuilder();

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.