开发者

array containing another

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");


That isn't possible in Java, I am afraid.

However you could use an ArrayList container which does have a constructor that copies all its contents from an Array. As a bonus ArrayList resize themself if you add more elements, whereas an Array does not.


Use the apache commons lang library to concatenate the arrays.

String[] tab1 ={"AF","HB,"ER"} 
String[] tab2 ={"HO","NF","BB","CD","PO"}

String[] concat = (String[]) ArrayUtils.addAll(tab1, tab2);


Arrays.asList(...).contains(...) 

this is the best solution you can use, if u dont want to go for cocatination. but i still feel kmb35 way is truely the best one

    String[] tab1 ={"AF","HB,"ER"}    
    String[] tab2 ={"AF","HB,"ER","HO","NF","BB","CD","PO"}
    String[] concat = (String[]) ArrayUtils.addAll(tab1, tab2);   

Dont forget to use the apache library. You can also prefer tab2 as

    private static final Set<String> tab2 = new HashSet<String>(Arrays.asList(
        new String[] {...//tab1 elements//...)); 


public class TestBoth {

    public static String[] join(String[]... params) {
        // calculate size of target array
        int size = 0;
        for (String[] array : params) {
            size += array.length;
        }

        String[] result = new String[size];

        int j = 0;
        for (String[] array : params) {
            for (String s : array) {
                result[j++] = s;
            }
        }
        return result;
    }

    public static void main(String[] args) {

        String[] tab1 = {"AF","HB", "ER"};

        String[] mtab = {"HO","NF","BB","CD","PO"};

        String[] tab2 = (String[]) join(tab1, mtab);

        System.out.println(java.util.Arrays.toString(tab2));
        /*
         * output : [AF, HB, ER, HO, NF, BB, CD, PO]
         */
    }
}


Create a TreeSet (sorted set) and then assign it to arrays.

TreeSet<String> tabSet = new TreeSet<String>();
tabSet.add("e1");
// rest of tab1 elements

String [] tab1 = Collections.toArray(tabSet);

tabSet.add("e2"); 
// add missing tab2 elements

String [] tab2 = Collections.toArray(tabSet);


I'd probably do it the way Jigar Joshi suggested, like so:

import java.util.Arrays;
import java.util.ArrayList;

class Test {
    public static void main(String[] args) {
        ArrayList<String> tab1 = new ArrayList<String>(Arrays.asList("AF", "HB", "ER"));

        ArrayList<String> tab2 = new ArrayList<String>(tab1) {{
            addAll(Arrays.asList("HO", "NF", "BB", "CD", "PO"));
        }};
    }
}

A bit weird syntax, I know. Maybe some people won't like it but it's compact, so I do.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜