-3

How would you remove a letter from a nested array in Java?

The nested array:

ArrayList arrayList = new ArrayList(
    Arrays.asList("H", "e", "l", "l", "o",
        new ArrayList(Arrays.asList( "w", "e", "l", "t",
        new ArrayList(Arrays.asList( "l", "e", "m", "o", "n", null)),
                            "c", "e", "l", l", true, "l")),
                3, 5, "!", "e")  );
Joe
  • 4,143
  • 8
  • 37
  • 65
  • It is unclear what the desired result is. Should all occurences of the same letter be removed? Should they be overwritten with an "empty" value or should the arrays be shortened or replaced by shorter arrays. Should the implementation be a function taking and returning arrays or be working on the existing arrays to modify them? These things must be clarified in the question. Especially if you intend to provide a solution, because if the question only gets clear by looking at your solution it would be "unfair competition" and give a very bad impression of your motives. Please read [ask]. – Yunnosch Aug 16 '20 at 18:03
  • Talk about nit-picky! This Q&A was posted as a general solution to a question that did not already have an answer on SO. As with any solution it can be modified to suit your use case. – Joe Aug 16 '20 at 18:10
  • To those who closed this question specifically what is the problem. I posted this as a Q&A to share knowledge. Is that not acceptable? How should you post a Q&A where you are providing the solution along with the question? – Joe Aug 16 '20 at 19:31
  • You should ask a question which in itself is clear enough and obviously interesting. Only asking a question as a stage for your answer is bot appreciated and is not accessable as an entry point to a problem, it cannot be found. I have described the things which are confusing when only reading the question. To make the special insight more obvious which your Q/A pair interesting, the actual specific problem which you want (and have solved) needs explanation. What is it that makes the use of `istanceof` helpful for the solution. Do not explain the goal it as "so that instanceof is needed". – Yunnosch Aug 16 '20 at 20:34
  • I updated the post's title, is that what you are looking for. There was no answer to this question on SO which is why I posted my question and answer after I figured it out. There is already 12 up votes on this QA posted today, that I assume is being blocked by your closure, so how can you say that it can't be found or is uninteresting. Others do not think that it is not appreciated or not accessible. I posed a clear question: How to ......? and a clear answer (use recursion and 'instanceof') with code. By what measure is this not clear? – Joe Aug 16 '20 at 21:32
  • I am confused. What do you mean by "There is already 12 up votes on this QA posted today" ? I (and also you, having the privilege of seeing vote counts) see 4 downvotes and 2 upvotes, for a score of -2 on your question and a 0 on your answer. So most people who happened on your QA were disappointed and neither your answer nor especially your question was well received. The total reputation you got from this is 12 (10+10-4*2). Maybe you are confusing this. – Yunnosch Aug 16 '20 at 21:45
  • I propose to describe in the questions body what is special about your nested array, i.e. that it is non-homeogenously nested. This is the reason why `instanceof` is needed, but reading that from the definition code or from the answers code is really not a way to find this specific problem description. But I start doing your work. If you do not decide to do it yourself then that is your perfect right. Feel free. I on the other hand will take the freedom to not reopen-vote the question as it is and leave the reopening to others, who you probably will convince. Good luck. – Yunnosch Aug 16 '20 at 21:48
  • When I click on the trophy icon I see +8 on the answer and +4 on the question and no -'s. In general you seem to be taking an extreme academic approach, which is fine if you want to go there but that is your job not mine if you choose to do so. I'm taking an approach of how do I solve this practical problem. IMHO it is not appropriate for you to try and enforce your approach on others. – Joe Aug 16 '20 at 22:06

1 Answers1

0

You can remove a letter from a nested array using recursion and instanceof.

static void removeLeterNestedArrayTest() {
    ArrayList arrayList = new ArrayList(
            Arrays.asList("H", "e", "l", "l", "o",
                    new ArrayList(Arrays.asList( "w", "e", "l", "t",
                            new ArrayList(Arrays.asList( "l", "e", "m", "o", "n", null)),
                            "c", "e", "l", "l", true, "l")),
                            3, 5, "!", "e")  );
    displayArrayList (arrayList);
    System.out.println();
    removeLetterNestedArray(arrayList, "l");
    displayArrayList (arrayList);
}
static void removeLetterNestedArray(ArrayList arrayList, String s) {
    for (Object obj:arrayList) {
        if (obj != null && obj instanceof ArrayList) {
            removeLetterNestedArray((ArrayList) obj, s);
        }
    }
    for (int i = 0; i < arrayList.size(); i++) {
        if (arrayList.get(i) != null && arrayList.get(i).toString().equals(s))
        { arrayList.remove(i);    i--; }
    }
}
static void displayArrayList (ArrayList arrayList) {
    for(Object obj: arrayList) {
        if (obj != null) {
            if (obj instanceof ArrayList)
                displayArrayList((ArrayList)obj);
            else System.out.print(obj.toString()); }
        else System.out.print("null");
    }
    System.out.println();
}

Joe
  • 4,143
  • 8
  • 37
  • 65
  • 1
    Also, [the usage of raw types is highly discouraged](https://stackoverflow.com/questions/2770321/what-is-a-raw-type-and-why-shouldnt-we-use-it). – Turing85 Aug 16 '20 at 18:01
  • Maybe this is an instance of [Moore's law](https://meta.wikimedia.org/wiki/Cunningham%27s_Law)? Post a wrong answer to receive a right one? – Turing85 Aug 16 '20 at 18:11
  • @Turing85 in this basic use scenario there is no need for strong typing. Notice the int and boolean in the array are simply ignored. I think you are over thinking, no need for the snark. – Joe Aug 16 '20 at 18:14
  • @Turing85 Those are good points. I fixed the typo, added a null to the array, added null checking and .equals – Joe Aug 16 '20 at 18:57