ArrayList的lastIndexof

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

ArrayList lastIndexof

问题

import java.util.Scanner;
import java.util.*;
public class bracketMatch {

    public bracketMatch() {
    }
    
    
    public static void main (String[] args) {
        Scanner scan = new Scanner(System.in);
        System.out.println("enter seq");
        String[] seq = scan.nextLine().split("");
        ArrayList<String> temp = new ArrayList<>();
        int x = 0;
        String check = null;
        System.out.println(Arrays.toString(seq));	
        for(String brak : seq)
        {
            switch(brak)
            {
                case "(":
                    temp.add("(");
                    
                    break;
                case "[":
                    temp.add("[");
                    
                    break;
                case "{":
                    temp.add("{");
                    
                    break;
                case "<":
                    temp.add("<");
                    
                    break;
                case ")":
                    temp.add(")");
                    
                    break;
                case "]":
                    temp.add("]");
                    
                    break;
                case "}":
                    temp.add("}");
                    
                    break;
                case ">":
                    temp.add(">");
                    
                    break;
                    
            }
                
        }
    
        x =  temp.lastIndexOf("(");
        System.out.println(x);
        /*if(x != -1)
         {	
            temp.remove(check);
            temp.remove(x);
         }*/
                     
        System.out.println(temp.toString());
    
    }
    
}

以上代码用于匹配括号,但我遇到了 ArrayList 方法 lastIndexOf
然而,它没有获取正确的索引,而是像 indexOf 方法一样工作。

我应该使用堆栈或 linkedlist 代替 ArrayList 吗?

另外,关于迭代 arraylist 并删除其元素,是否可以提供帮助?因为迭代器和 foreach 会出错。

英文:
import java.util.Scanner;
import java.util.*;
public class bracketMatch {
public bracketMatch() {
}
public static void main (String[] args) {
Scanner scan = new Scanner(System.in);
System.out.println(&quot;enter seq&quot;);
String[] seq = scan.nextLine().split(&quot;&quot;);
ArrayList&lt;String&gt; temp = new ArrayList&lt;&gt;();
int x = 0;
String check = null;
System.out.println(Arrays.toString(seq));	
for(String brak : seq)
{
switch(brak)
{
case &quot;(&quot;:
temp.add(&quot;(&quot;);
break;
case &quot;[&quot;:
temp.add(&quot;[&quot;);
break;
case &quot;{&quot;:
temp.add(&quot;{&quot;);
break;
case &quot;&lt;&quot;:
temp.add(&quot;&lt;&quot;);
break;
case &quot;)&quot;:
temp.add( &quot;)&quot;);
break;
case &quot;]&quot;:
temp.add(&quot;]&quot;);
break;
case &quot;}&quot;:
temp.add(&quot;}&quot;);
break;
case &quot;&gt;&quot;:
temp.add(&quot;&gt;&quot;);
break;
}
}
x =  temp.lastIndexOf(&quot;(&quot;);
System.out.println( x);
/*  if(x != -1)
{	
temp.remove(check);
temp.remove(x);
}*/
System.out.println(temp.toString()) ;
}
}

The above code is for matchingbrackets, but I stumbled upon ArrayList method lastIndexOf.
However, it is not fetching the correct index and is working like indexOf method.

Should I use stack or linkedlist instead of ArrayList?

Also, any help on iterating an arraylist and removing its element, since iterator and foreach gives error.

答案1

得分: 0

你可以给我们提供更多信息吗?方法 lastIndexOf 正常工作,它会给你列表中对象的最后一个出现位置,它不像 indexOf 那样工作。如果你想删除 arraylist 中的最后一个 "(" 出现,只需取消注释你的 temp.remove(x) 行。

英文:

Can you give us some more information? Method lastIndexOf works properly, it gives you last occurence of object in list, it doesn't work like indexOf. If you want to delete last occurence of "(" from arraylist just uncomment your temp.remove(x) line.

huangapple
  • 本文由 发表于 2020年4月9日 15:58:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/61116456.html
匿名

发表评论

匿名网友

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

确定