I have an array tab1 that contains some strings and another array that could contains all element of array tab1. how do I can do this to avoid repeating same element of tab1 :
String[] tab1 ={"AF","HB,"ER"}
String[] tab2 ={"AF","HB,"ER","HO","NF","BB","CD","PO"}
I would like to say : tab2 = {tab1,"HO",...} any idea ?
thanks,
You might want to use Arrays.copyOf() to create an array of size tab1.length + 5, which starts with the elements of tab1, and then add [manually] elements of tab2
Simple example:
String[] tab1 ={"AF","HB","ER"};
String[] tab2 = Arrays.copyOf(tab1, tab1.length+5);
tab2[3] = "HO";
tab2[4] = "NF";
tab2[5] = "BB";
tab2[6] = "CD";
tab2[7] = "PO";
System.out.println(Arrays.toString(tab2));
[of course if you have the later elements in a third array you can iterate and add them]
how do I can do this to avoid repeating same element of tab1
You can Use List
, here there will be only one object for for example "AF"
with two reference
List<String> a = new ArrayList<String>;
List<String> b = new ArrayList<String>;
b.addAll(a);
b.add("HO");
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With