0

我在一个数组列表中有数字,其中还包含字母数字。例如。

1          1a
2          1b
3          2
4a         3
4b         4
4c         5a
4d   (or)  5b   (or) [in any sequence may be]
5          5c
6          6
7          7
8          8 
9a         9
9b         10
10      

我这是如何删除单个元素并重新排序整个列表。例如从上面的列表中我要删除 2,那么结果必须是。

1          1a
           1b
2           
3a         2
3b         3
3c         4a
3d   (or)  4b   (or) [in any sequence may be]
4          4c
5          5
6          6
7          7
8a         8
8b         9
9      

我这是如何删除单个元素并重新排序整个列表。例如,从上面的列表中,我将删除左侧列表中的 3a 和另一方面删除 1b(或)1a,那么结果必须是。

           1
1          
2           
           2
3a         3
3b         4a
3c   (or)  4b   (or) [in any sequence may be]
4          4c
5          5
6          6
7          7
8a         8
8b         9
9      

我希望这将解释我所需要的。如何实现这个????

4

1 回答 1

0

在此,您可以更改string idString and itemToDelete任何您喜欢更改的值。它将提供所需的输出。

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;


public class OrderTheListBasedOnItemDeleted {


    public static void main(String[] args) {

        List<String> idList = new ArrayList<String>();
        String idToBeDeleted="5a";

        String idString= "1,2a,2b,2c,3,4,5a,5b";

        String idArray[]=idString.split(",");

        for(int i=0;i<idArray.length;i++){
            idList.add(idArray[i]); 
        }


        int duplicateCount=0;
         List<Integer>indexOfItemsForReorder = new ArrayList<Integer>();

            Pattern patternDigit = Pattern.compile("\\d+",Pattern.CASE_INSENSITIVE);
            Pattern patternLetter = Pattern.compile("[a-zA-Z]+",Pattern.CASE_INSENSITIVE);
            String digit="";
            String letter="";
            String digit2="";
            String letter2="";


        for(String a :idList){
            System.out.println(a);
        }
        System.out.println();


        Matcher mD = patternDigit.matcher(idToBeDeleted);
        Matcher mL = patternLetter.matcher(idToBeDeleted);
        if (mD.find()) {
            digit  = mD.group();
        }
        if (mL.find()) {
            letter  = mL.group();
        }


        if(letter!=""){
                for(String idToBeDeleted : idList){

                            Pattern patternDigitLetter = Pattern.compile(digit+"[a-zA-Z]+",Pattern.CASE_INSENSITIVE);   
                            Matcher digitletterMatcher = patternDigitLetter.matcher(idToBeDeleted);
                            if(digitletterMatcher.find()){
                                indexOfItemsForReorder.add(idList.indexOf(digitletterMatcher.group()));
                                duplicateCount=duplicateCount+1;
                            }
                }
                int indexofContentToBeReplaced = indexOfItemsForReorder.get(indexOfItemsForReorder.size()-1);
                //for letter more than 2...
                if(duplicateCount>2){
                    idList.remove(indexofContentToBeReplaced);
                }else if(duplicateCount>=1&&duplicateCount<=2){
                    //for letter more than 1 and less than 2...

                    idList.remove(indexofContentToBeReplaced);
                    idList.remove(indexofContentToBeReplaced-1);
                    idList.add(indexofContentToBeReplaced-1, digit);


                }


                System.out.println("result...");

    }else{
            int indexOfItemToBeDeleted=idList.indexOf(idToBeDeleted);       
            List <String> newIdList = new ArrayList<String>();

            for(int i=0;i<indexOfItemToBeDeleted ;i++){

                newIdList.add(idList.get(i));
            }
            idList.remove(idToBeDeleted);

            for(int i=indexOfItemToBeDeleted; i<idList.size(); i++){
                digit2="";
                letter2="";
                Matcher mD2 = patternDigit.matcher(idList.get(i));
                Matcher mL2 = patternLetter.matcher(idList.get(i));
                if(mD2.find()){
                    digit2=mD2.group();
                }

                if (mL2.find()) {
                    letter2  = mL2.group();
                }

                int d=Integer.parseInt(digit2);
                d=d-1;
                newIdList.add(i, Integer.toString(d)+letter2);

            }

            if(newIdList.contains("0")){
                newIdList.remove("0");
            }


            System.out.println("result...");
            idList.clear();
            idList=newIdList;
    }

        for(String a :idList){
            System.out.println(a);
        }


    }
}

输出

Input
1
2a
2b
2c
3
4
5a
5b

result...
1
2a
2b
2c
3
4
5
于 2012-08-14T07:18:23.047 回答