Java: 如何从嵌套数组中移除一个字母

huangapple go评论79阅读模式
英文:

Java: How do you remove a letter from a nested Array

问题

// 导入所需的类
import java.util.ArrayList;
import java.util.Arrays;

// 创建嵌套数组
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"));

// 从嵌套数组中删除一个元素
((ArrayList) ((ArrayList) ((ArrayList) arrayList.get(5)).get(4))).remove(5);
英文:

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

答案1

得分: 0

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();
}
英文:

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

static void removeLeterNestedArrayTest() {
ArrayList arrayList = new ArrayList(
Arrays.asList(&quot;H&quot;, &quot;e&quot;, &quot;l&quot;, &quot;l&quot;, &quot;o&quot;,
new ArrayList(Arrays.asList( &quot;w&quot;, &quot;e&quot;, &quot;l&quot;, &quot;t&quot;,
new ArrayList(Arrays.asList( &quot;l&quot;, &quot;e&quot;, &quot;m&quot;, &quot;o&quot;, &quot;n&quot;, null)),
&quot;c&quot;, &quot;e&quot;, &quot;l&quot;, &quot;l&quot;, true, &quot;l&quot;)),
3, 5, &quot;!&quot;, &quot;e&quot;)  );
displayArrayList (arrayList);
System.out.println();
removeLetterNestedArray(arrayList, &quot;l&quot;);
displayArrayList (arrayList);
}
static void removeLetterNestedArray(ArrayList arrayList, String s) {
for (Object obj:arrayList) {
if (obj != null &amp;&amp; obj instanceof ArrayList) {
removeLetterNestedArray((ArrayList) obj, s);
}
}
for (int i = 0; i &lt; arrayList.size(); i++) {
if (arrayList.get(i) != null &amp;&amp; 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(&quot;null&quot;);
}
System.out.println();
}

huangapple
  • 本文由 发表于 2020年8月17日 01:58:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/63440260.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定