4

I want to copy only "b", "d" and "e" from arr1 to arr2 and increase the size of arr2 dynamically while adding.

I tried adding a if condition

 if(!arr1[i].equals("a") && !arr1[i].equals("c")) { arr2 = Arrays.copyOf(arr1, i + incrementLength); } 

but it still copies everything from arr1 to arr2.

import java.util.Arrays; public class Test { public static void main(String[] args) { String[] arr1 = new String[] { "a", "b", "c", "d", "e" }; String[] arr2 = new String[0]; int incrementLength = 1; for (int i = 0; i < arr1.length; i++) { if(!arr1[i].equals("a") && !arr1[i].equals("c")) { arr2 = Arrays.copyOf(arr1, i + incrementLength); } } for (String value : arr2) { System.out.println(value); } } } 
5
  • Is there a reason why you favor arrays over ArrayList? Do you mind using Streams? Commented Dec 20, 2016 at 9:58
  • learning arrays. ok it's actually a challenge by my professor. he won't allow me to use Arraylist. Commented Dec 20, 2016 at 10:04
  • 2
    Then hint: read the javadoc for the library calls you are using carefully. Commented Dec 20, 2016 at 10:05
  • I would go for Stream, filter, toArray then ;-) Commented Dec 20, 2016 at 10:06
  • I think I would do it the other way around: create an array the same size as the source array (5 in the example). Copy elements in there, keeping track of how many have already been copied so I know where to put the next. At the end use Arrays.copyOf() to truncate the array to fit the elements in there. Commented Dec 20, 2016 at 10:35

4 Answers 4

2

The Arrays.copyOf(arr1, i + incrementLength); in your code is creating unnecessary multiple copies of arr1 inside the loop. Something that you should avoid doing.

Take a look at this Documentation from Oracle. The second parameter is the number of elements from arr1 that you will be copying to arr2.

Instead, you can add the needed elements to a List and obtain a copy of the array from that list. Something like this:

 List<String> list = new ArrayList<>(); for (int i = 0; i < arr1.length; i++) { if(!arr1[i].equals("a") && !arr1[i].equals("c")) { list.add(arr[i]); } } arr2 = list.toArray(new String[list.size()]); 

Update

If you do not wish to use Lists, you can try this:

 String[] arr1 = new String[] { "a", "b", "c", "d", "e" }; String[] arr2 = new String[arr1.length]; int j = 0; int finalLength = 0; for (int i = 0; i < arr1.length; i++) { if(!arr1[i].equals("a") && !arr1[i].equals("c")) { // add only elements that you need arr2[j++] = arr1[i]; // keep track of how many elements you have added finalLength++; } } // truncate array to finalLength String[] arr3 = Arrays.copyOf(arr2, finalLength); for (String value : arr3) { System.out.println(value); } 

This generates the following output:

 b d e 

Hope this helps!

Sign up to request clarification or add additional context in comments.

Comments

1

If you want to copy individual elements of the source array, you can't use Arrays.copyOf. Had you copied a continuous sub-range of the array, you could have used System.arraycopy, but that's not what you are doing. Arrays.copyOf can only be used to either copy the entire array or a prefix of the array (i.e. all the elements from the first element until some index).

You have to first calculate the required length of the output array, instantiate the output array and then iterate over the elements of the input array and copy the relevant elements one by one to the output array.

public static void main(String[] args) { String[] arr1 = new String[] { "a", "b", "c", "d", "e" }; int newLen = 0; for (int i = 0; i < arr1.length; i++) { if(!arr1[i].equals("a") && !arr1[i].equals("c")) { newLen++; } } String[] arr2 = new String[newLen]; int index = 0; for (int i = 0; i < arr1.length; i++) { if(!arr1[i].equals("a") && !arr1[i].equals("c")) { arr2[index++] = arr1[i]; } } for (String value : arr2) { System.out.println(value); } } 

This has a disadvantage of iterating over the input array twice, since you can't create the output array until you know how many elements it should contain. You can avoid that by adding the selected elements of the input array to an ArrayList<String>, and then convert the ArrayList to an array.

Comments

1

Can also be done using Streams:

String[] arr1 = new String[] { "a", "b", "c", "d", "e" }; String[] arr2 = Stream.of(arr1) .filter(s -> !s.equals("a") && !s.equals("c")) .toArray(String[]::new); 

Print the result:

Stream.of(arr2) .forEach(System.out::println); 

Gives:

b d e 

Comments

0

You'd better use another method copyOfRange:

https://docs.oracle.com/javase/7/docs/api/java/util/Arrays.html#copyOfRange(char[],%20int,%20int)

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.