从Java中的String数组访问previous和next元素

时间:2017-06-01 14:30:00

标签: java arrays string

对于存储在Java中的字符串数组中的给定输入"<Hello>/What <I am>/Who <fine>/SKIP",例如:

String arr[] = {"<","Hello",">","/What","<","I","am",">","/Who","<","fine",">","/SKIP"};

如何以下面给出的格式获得输出:

预期产出:

Hello \tab What\newline   
I     \tab -\newline   
am    \tab Who\newline   
fine  \tab SKIP\newline   

我的代码是:

public class ArrayTest {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        String arr[] = {"<","Hello",">","/What","<","I","am",">","/Who","<","fine",">","/SKIP"};
        String current=null;
        String next =null;
        for(int i=0;i<arr.length;i++){

            current = arr[i];
            next = arr[i+1];
            if(current.equals("<")){

            }
            else if (!(current.equals(">"))&& !(next.equals("/Who"))|!(next.equals("/What"))
                    |!(next.equals("Who"))|!(next.equals("/SKIP"))){

                System.out.println(current+"\t"+"-");
            }
            else if ((current.equals(">"))&& (next.equals("/Who"))){

                System.out.println(current+"\t"+"-");
            }
            else {
                System.out.println(arr[i]+"\t"+"-");
            }
            //System.out.println(arr[i+4]);
        }
    }

}

0 个答案:

没有答案