java中的编号序列排序,用于删除时的字母数字

时间:2012-06-20 05:32:27

标签: java numbers

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

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      

我希望这能解释我需要的东西。如何实现这个????

1 个答案:

答案 0 :(得分: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