简体   繁体   English

逗号分隔字符串中的数字范围

[英]number range in comma separated string

I have to create a method that allow user to create offer id range like follows ... 我必须创建一个允许用户创建商品ID范围的方法,如下所示...

3,4,5 3, ,6 3,102, 3,104,* 3,4,5 3, ,6 3,102,3,104,*

in the above examples there are three type of format (ie x,x,x OR x, ,x OR x,x, ), where x is a number in asc order. 在以上示例中,存在三种类型的格式(即x,x,x OR x,, xOR x,x, ),其中x是升序的数字。

my problem is in i have to make sure that user is not able to create offer id range with overlap id range. 我的问题是我必须确保用户无法创建具有重叠ID范围的商品ID范围。 for example ... 例如 ...

if offer id 3,102,* already created than user not able to create following combinations ... 3,102,105 similarly if offer id range 3,* is already exists than user not able to create 3,4 Or 3,5 OR 3,10,11 like combinations. 如果商品ID 3,102,*比不能创建以下组合的用户创建了... 3,102,105如果商品ID范围3,*已经存在,则比不能创建3,4或3,5或3,10,11的用户相似像组合。

i am not able to complete my following method 我无法完成以下方法

String[] digits1 = range1.split(",");
            String[] digits2 = range2.split(",");
            int digits1Len = digits1.length;
            int digits2Len = digits2.length;

            if (digits1[digits1Len -1].equals("*")) {
                --digits1Len; 
            }
            if (digits2[digits2Len -1].equals("*")) {
                --digits2Len; 
            }

            String[] digits1_1 = new String[digits1Len];
            String[] digits2_2 = new String[digits2Len];;
            System.arraycopy(digits1, 0, digits1_1, 0, digits1Len);
            System.arraycopy(digits2, 0, digits2_2, 0, digits2Len);

            if(!Arrays.asList(digits1_1).contains("*") || !Arrays.asList(digits2_2).contains("*")) {
                if (digits1_1[digits1Len -1].equals(digits2_2[digits2Len -1])) {                        
                    if(digits1Len != digits2Len) {
                        result = false;
                    } else{
                        // Loop                         
                    }
                } else if(false/*Lenght of the array with higher element should be more by 1 as compared to ther array*/) {

                }   
            } else {
                String [] normalPatternArr = null;
                String [] oldPatternArr = null;
                if(Arrays.asList(digits1_1).contains("*")) {
                    if (digits1_1.length < digits2_2.length) {
                        result = false;
                    } else {
                        normalPatternArr = digits2_2;
                        oldPatternArr = digits1_1;
                    }
                } else if(Arrays.asList(digits2_2).contains("*")) {
                    if (digits2_2.length < digits1_1.length) {
                        result = false;
                    } else {
                        normalPatternArr = digits1_1;
                        oldPatternArr = digits2_2;
                    }
                } else {                        
                    for (int i=0; i < normalPatternArr.length; i++) {
                        if (!normalPatternArr[i].equals(oldPatternArr[i])) {
                            if (!oldPatternArr[i].equals("*")) {
                                result = false;                                 
                                return result;
                            } else {

                            }
                        }
                    }
                }                   
            }                   

I think an easier way is to keep it as a String 我认为更简单的方法是将其保留为字符串

Eg 例如

String orig = "3,4,5,*";

becomes 变成

String mask = "3,4,5,";

if (secondString.startsWith (mask)) 
    // say no

but there again, maybe you are not explaining your problem correctly 但是又一次,也许您没有正确解释问题

This class stores one triple of these ids: 此类存储以下ID的三分之一:

public class IdRange {
private Integer[] triple = new Integer[3];
public IdRange(Integer a, Integer b, Integer c ){
    this.triple[0] = a;
    this.triple[1] = b;
    this.triple[2] = c;
}
public Integer getTriple( int i ){ return triple[i]; }
public boolean overlap( IdRange other ){
for( int i = 0; i < 3; i++ ){
    if( this.triple[i] == null ||
            other.getTriple( i ) == null ) return true;
    }
    return false;
}
public String toString(){
StringBuilder sb = new StringBuilder();
    if( triple[0] == null ) sb.append( '*' );
    else sb.append( triple[0] );
    sb.append( ',' ); 
    if( triple[1] == null ) sb.append( '*' );
    else sb.append( triple[1] );
    sb.append( ',' ); 
    if( triple[2] == null ) sb.append( '*' );
    else sb.append( triple[2] );
    return sb.toString();
}
}

This class parses a string containing a triple with missing or asterisked values and checks for overlaps: 此类解析包含缺少值或星号的三元组的字符串,并检查是否存在重叠:

public class Ranges {
private List<IdRange> rangeList = new ArrayList<>();
public boolean parseAndCheck( String s ){
String[] abc = s.split( "," );
    Integer a = null;
    Integer b = null;
    Integer c = null;
    if( abc.length == 3 &&
        ! "*".equals( abc[2] ) &&
        ! "".equals( abc[2] ) ){
        c = Integer.valueOf( abc[2] );
    }
    if( abc.length >= 2 &&
        ! "*".equals( abc[1] ) &&
        ! "".equals( abc[1] ) ){
        b = Integer.valueOf( abc[1] );
    }
    if( abc.length >= 1 &&
        ! "*".equals( abc[0] ) &&
        ! "".equals( abc[0] ) ){
        a = Integer.valueOf( abc[0] );
    }

    IdRange nrng = new IdRange( a, b, c );
    for( IdRange r: rangeList ){
        if( r.overlap( nrng ) ) return true;
    }
    rangeList.add( nrng );
    return false;
}

public void dump(){
    for( IdRange r: rangeList ){
        System.out.println( r );
    }
}

public static void main( String[] args ){
    Ranges ranges = new Ranges();
    for( String s: args ){
        if( ranges.parseAndCheck( s ) ){
            System.out.println( "overlap: " + s );
        }
    }
    ranges.dump();
}
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM