0
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. 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?

2
  • Static things are not inherited. So having static arrays you can't. Commented Jan 15, 2014 at 4:48
  • Personally, I'd set up a static function and have the static initializers call that. Or have the class constructor call it to set them if necessary. Commented Jan 15, 2014 at 4:51

2 Answers 2

3
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.

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

1 Comment

Thanks for teaching me something (that I probably will never use) today.
2

You can use ArrayList rather than String Array, since its very difficult to remove/insert elements if you use String array.

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. Find that element and save&&remove it.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.