Reputation: 77
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);
}
}
}
Upvotes: 4
Views: 2551
Reputation: 23242
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
Upvotes: 1
Reputation: 6721
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!
Upvotes: 2
Reputation: 10562
You'd better use another method copyOfRange
:
https://docs.oracle.com/javase/7/docs/api/java/util/Arrays.html#copyOfRange(char[],%20int,%20int)
Upvotes: 0
Reputation: 393781
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.
Upvotes: 1