Reputation:
What I need to do is create a third dynamic array (I think I did) that combines the length of the first two and then prints out the contents of those arrays as the output of the third array.
Example:
Array 1: 1 4 2 6 0
Array 2: 3 6 2 1 5
Array 3: 1 4 2 6 0 3 6 2 1 5
This is what I have so far:
import javax.swing.*;
public class DynamicArray
{
public static void main(String[] args)
{
// User Input - Array length
int arrayLength;
arrayLength = Integer.parseInt(JOptionPane.showInputDialog("Enter Array length:"));
// First dynamic array
int[] dynArray1;
dynArray1 = new int[arrayLength];
for (int i = 0; i < dynArray1.length; i++)
{
dynArray1[i] = (int) (Math.random() * 10);
}
System.out.println("First Array:");
for (int j = 0; j < dynArray1.length; j++)
{
System.out.println(dynArray1[j]);
}
// Second dynamic array
int[] dynArray2;
dynArray2 = new int[arrayLength];
for (int i = 0; i < dynArray2.length; i++)
{
dynArray2[i] = (int) (Math.random() * 10);
}
System.out.println("Second Array:");
for (int j = 0; j < dynArray2.length; j++)
{
System.out.println(dynArray2[j]);
}
// Third dynamic array
int thirdArrayLength = dynArray1.length + dynArray2.length;
int[] dynArray3;
dynArray3 = new int[thirdArrayLength];
for (int i = 0; i < dynArray3.length; i++)
{
}
}
}
I would just print the first and second but I think that would be cheating and it isn't what my assignment is asking for. I was going to for loop Array1 and Array2 and println the results. I'm not exactly sure how I should go about it though. Any help would be appreciated.
EDIT: This has been solved! Below is the code:
/* Third dynamic array */
int thirdArrayLength = dynArray1.length + dynArray2.length;
int[] dynArray3;
dynArray3 = new int[thirdArrayLength];
for (int i=0; i < dynArray1.length; i++) {
dynArray3[i] = dynArray1[i];
}
for (int i=0; i < dynArray2.length; ++i) {
dynArray3[i + dynArray1.length] = dynArray2[i];
}
System.out.println("Third Array:");
for (int i=0; i < dynArray3.length; ++i) {
System.out.println(dynArray3[i]);
}
Upvotes: 1
Views: 114
Reputation: 30809
You can use Java 8's stream
to sequencially iterate over two arrays, e.g.:
public static void main(String[] args) throws Exception{
int[] array1 = {1,2,4}, array2 = {4,5,6};
Stream.of(array1, array2)
.flatMapToInt(s -> Arrays.stream(s))
.forEach(System.out::println);
}
Update
If you want to merge/concat two arrays then you add a Collector
in this stream which would generate a list, e.g.:
public static void main(String[] args) throws Exception{
int[] array1 = {1,2,4}, array2 = {4,5,6};
List<Integer> list = Stream.of(array1, array2)
.flatMapToInt(s -> Arrays.stream(s))
.boxed()
.collect(Collectors.toList());
}
This would give you the list of Integer
, you can then convert it into int[]
array like this:
int[] array = new int[list.size()];
for(int i=0 ; i<list.size() ; i++){
array[i] = list.get(i);
}
Upvotes: 0
Reputation: 5055
There are two (+1) possibilities.
Loop over destination array
You can loop over the third array once (as you currently did), and use an if
, which helps you to determine, whether you should access the first or second array.
if (i > array1.length) {
array3[i] = array2[i - array1.length];
else
array3[i] = array1[i];
}
Loop over both source arrays
Or you can use two dedicated loops which loop over the first and second array and assign their values to the third. The second loop which iterates over array2
has to use an offset for indexing the third array, though.
array3[i + array1.length] = array2[i];
Java8's Stream API
Use IntStream concat(IntStream a, IntStream b)
in IntStream
to concatenate two streams, which you can create with java.util.Arrays.stream(int[])
.
Upvotes: 0
Reputation: 59950
If you are using Java-8 then you can use Arrays
and System.arraycopy
:
//your array
int array1[] = {1, 4, 2, 6, 0};
int array2[] = {3, 6, 2, 1, 5};
//create a new array witch have length array1.length + array2.length
int array3[] = new int[array1.length + array2.length];
//copy the first array1 in array3
System.arraycopy(array1, 0, array3, 0, array1.length);
//copy the second array2 in array3
System.arraycopy(array2, 0, array3, array1.length, array2.length);
//display your arrays
System.out.println("Array 1:" + Arrays.toString(array1));
System.out.println("Array 2:" + Arrays.toString(array2));
System.out.println("Array 3:" + Arrays.toString(array3));
Output
Array 1:[1, 4, 2, 6, 0]
Array 2:[3, 6, 2, 1, 5]
Array 3:[1, 4, 2, 6, 0, 3, 6, 2, 1, 5]
Upvotes: 1
Reputation: 604
There's a nice Apache Commons library method for this: http://commons.apache.org/proper/commons-lang/javadocs/api-3.1/org/apache/commons/lang3/ArrayUtils.html#addAll%28T%5B%5D,%20T...%29
int[] dynArray3 = (int[])ArrayUtils.addAll(dynArray1, dynArray2);
for(int i : dynArray3) {
System.out.println(i);
}
Upvotes: 0
Reputation: 5916
You can create the third array as soon as the user inputs the first two arrays' length, then in the two for
loops where you print their content, you can also populate the third array.
In this case you will have to pay attention at the second for
's counter, which will start from zero but will have to be increased because a part of the third array will already be populated.
I won't say more, since you said it's homework and don't want to cheat :)
Upvotes: 0
Reputation: 520888
One option is to just iterate over the first two arrays and populate the third array:
for (int i=0; i < dynArray1.length; i++) {
dynArray3[i] = dynArray1[i];
}
for (int i=0; i < dynArray2.length; ++i) {
dynArray3[i + dynArray1.length] = dynArray2[i];
}
// then print out the third array
for (int i=0; i < dynArray3.length; ++i) {
System.out.println(dynArray3[i]);
}
Upvotes: 0