简体   繁体   English

在Java /继承之类的概念中重用静态字符串数组

[英]Reuse static string array in Java / Inheritance like concept inside string array

private static String[][] s1 = {
        { "a", "a1" },
        { "b", "b1" },
        { "c", "c1" },
        { "d", "d1" },
}

private static String[][] s2 = {
        { "c", "c1" },
        { "d", "d1" },
        { "e", "e1" },
        { "f", "f1" },
}

Here c,c1 and d,d1 is repeated in those string arrays. 在此,在这些字符串数组中重复c,c1和d,d1。 How to take that outside and put it in a common string array say s3, and s1 and s2 reuses/inherits it so that it need not be declared in two different places? 如何将其带到一个外部字符串中,例如s3,以及s1和s2重用/继承它,这样就不必在两个不同的地方声明它了?

static String[] common = { "c", "c1" };

private static String[][] s1 = {
    { "a", "a1" },
    { "b", "b1" },
    common,
    { "d", "d1" }
}

private static String[][] s2 = {
    common,
    { "d", "d1" },
    { "e", "e1" },
    { "f", "f1" }
}

Works fine. 工作正常。

You can use ArrayList rather than String Array, since its very difficult to remove/insert elements if you use String array. 您可以使用ArrayList而不是String Array,因为如果使用String数组,很难删除/插入元素。

ArrayList<String[]> s1=new ArrayList<String[]>();
ArrayList<String[]> s2=new ArrayList<String[]>();

And use add() method to add all the elements, and scan the lists to find out the overlapped elements by using contains() method. 然后使用add()方法添加所有元素,并使用contains()方法扫描列表以找出重叠的元素。 Find that element and save&&remove it. 找到该元素并保存并删除。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM