如何解析集合的字符串? [英] How can I parse a string for a set?

查看:91
本文介绍了如何解析集合的字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一种方法,该方法应该采用格式为"s1:{1,2,3,4}"的输入String并将其放入Set中.我自己开发的集合类如下:

I am writing a method that is supposed to take an input String of the format "s1:{1,2,3,4}" and put it into a Set. The set class I have developed myself, and is as follows:

public class Set<E> implements Iterable<E> {
    private static final int DEFAULT_CAPACITY = 20;
    private String name;
    private E[] theData;
    private int size = 0;
    private int capacity = 0;

    public Set(){
        capacity = DEFAULT_CAPACITY;
        theData = (E[]) new Object[capacity];
    }//end constructor

    public Set(String name){
        capacity = DEFAULT_CAPACITY;
        theData = (E[]) new Object[capacity];
        this.name = name;
    }//end constructor

    public String getName(){
        return name;
    }//end getName

    public void setName(String name){
        this.name = name;
    }//end setName

    //adds object to set
    public void add(Object E) {
        if (size == capacity) {
            reallocate();
        }//end if

        theData[size] = (E) E;
        size++;

        for (int j = 0; j<size; j++) {
            for (int k = 0; k < size; k++) {
                if ((int)theData[j] < (int)theData[k]) {
                    E temp = theData[j];
                    theData[j] = theData[k];
                    theData[k] = temp; 
                }//end if
            }//end nested for loop
        }//end for loop

        int counter = 0;
        for (int i = 0; i < size; i++) {
            if (E == theData[i]) {
                counter++;

                if (counter >= 2) {
                    remove((Object)E);
                 }//end nested if
            }//end if
        }//end for loop
    }//end add method

    public E get(int i) {
        if (i < 0 || i >= size) {
            throw new ArrayIndexOutOfBoundsException(i);
        } else {
            return theData[i];
        }//end else 
    }//end get method

    public E remove(int i) {
        if (i < 0 || i >= size) {
            throw new ArrayIndexOutOfBoundsException(i);
        }//end if

        E returnValue = theData[i];
        for (int j = i + 1; j < size; j++) {
            theData[j - 1] = theData[j];
        }//end for loop

        size--;
        return returnValue;
    }//end remove method

    public void remove(Object E) {
        for (int i = 0; i < size; i++) {
            if (E == theData[i]) {
                for (int j = i + 1; j < size; j++){
                    theData[j - 1] = theData[j];
                }//end nested for loop

                size--;
            }//end if
        }//end for loop
    }//end remove method

    //fix!
    public int find(Object E) {
        int first, last, middle;
        first = 0;
        last = size - 1;
        middle = (first+last) / 2;

        while(first <= last ) {
            if ((int)theData[middle] > (int)E ) {
                last = middle - 1;    
            } else if ((int)theData[middle] < (int)E ) {
                first = middle + 1;
            } else {
                return middle;
            }//end else
        }//end while

        if (first > last) {
            return -1;
        }//end if

        return -1;
    }//end find method

    public Set<E> union(Set<E> s) {
        Set<E> returnSet = new Set<E>();

        for (int i = 0; i < this.size; i++) {
            returnSet.add(this.theData[i]);
        }//end for loop

        for (int i = 0; i < s.size; i++) {
            returnSet.add(s.theData[i]);
        }//end for loop

        return returnSet;
    }//end union method

    public Set<E> intersect(Set<E> s) {
        Set<E> returnSet = new Set<E>();

        for (int i = 0; i < this.size; i++) {
            for (int j = 0; j < s.size; j++) {
                if (this.theData[i] == s.theData[j]){
                    returnSet.add(theData[i]);
                }//end if
            }//end nested for loop
        }//end for loop

        return returnSet;
    }//end intersect method

    public Set<E> subtract(Set<E> s) {
        Set<E> returnSet = new Set<E>();

        for (int i = 0; i < this.size; i++) {
            for (int j = 0; j < s.size; j++) {
                if (this.theData[i] == s.theData[j]) {
                    this.remove((Object)this.theData[i]);
                    s.remove((Object)s.theData[j]);
                }//end if
            }//end nested for loop
        }//end for loop

        for (int i = 0; i < this.size; i++) {
            returnSet.add(this.theData[i]);
        }//end for loop

        for (int i = 0; i < s.size; i++) {
            returnSet.add(s.theData[i]);
        }//end for loop

        return returnSet;
    }//end subtract method

    public boolean equals(Set<E> s) {
        boolean result = false;

        for (int i = 0; i < this.size; i++) {
            if (this.theData[i] == s.theData[i]) {
                result = true;
            }//end if

            if (this.theData[i] != s.theData[i]) {
                result = false;
                break;
            }//end if
        }//end for loop

        return result;
    }//end equals method


    private void reallocate() {
        capacity = 2*capacity;
        theData = Arrays.copyOf(theData,  capacity);
    }//end reallocate method

    public String toString() {
        StringBuilder set = new StringBuilder();
        set.append("{");

        for (int i = 0; i < size; i++) {
            set.append(theData[i]);

            if (i != size-1){
                set.append(",");
            }//end if
        }//end for loop

        set.append("}");

        return set.toString();
    }//end toString()

    public SetIterator<E> iterator() {
        SetIterator<E> it = new SetIterator<E>() {
            private int currentIndex = 0;

            public boolean hasNext() {
                if (currentIndex < size && theData[currentIndex] != null){
                    currentIndex++;
                    return true;
                } else{
                    return false;
                }//end else
            }//end hasNext()


            public E next() {
                if (!hasNext()) {
                    throw new NoSuchElementException();
                }//end if

                return theData[currentIndex++];
            }//end next()


            public boolean hasPrevious() {
                if (currentIndex <= size && currentIndex > 0) {
                    currentIndex--;
                    return true;
                } else {
                    return false;
                }//end else
            }//end hasPrevious()


            public E previous() {
                if (!hasPrevious()) {
                    throw new NoSuchElementException();
                }//end if

                return theData[currentIndex--];
            }//end previous()


            public void add(E item) {
                theData[currentIndex-1] = item;
            }//end add()


            public void remove() {
                for (int i = 0; i < size; i++) {
                    if (theData[currentIndex] == theData[i]) {
                        for (int j = i + 1; j < size; j++) {
                            theData[j - 1] = theData[j];
                        }//end nested for loop

                        size--;
                    }//end if
                }//end for loop
            }//end remove()
        };//end new SetIterator()

        return it;
    }//end iterator method
}//end Set class

方法应为

    如果方法的格式无效,例如"s1:[1 2,3,4}"(此示例为缺少逗号和花括号),则
  • 引发异常.
  • 此外,输入可以具有任意数量的空格,并且仍然被认为是有效的.例如:"s1: {1, 2, 3, 4 }".
  • throw an exception if the method has invalid format such as "s1:[1 2,3,4}" (This example being the missing comma and curly brace).
  • additionally, the input may have any number of whitespaces and will still be considered valid. Example: "s1: {1, 2, 3, 4 }".

到目前为止,我所拥有的方法是:

So far all I have for the method is:

public Set<Integer> parse(String input){
    String s[] = input.split(":");
    String name = s[0];
    Set<Integer> returnSet = new Set<Integer>(name);

    return returnSet;
}

对于如何从字符串集中的集合中正确检索元素并将它们放入Set对象中,我不确定.我知道一旦我自己得到它们就可以parseInt,但是在隔离每个元素时遇到了麻烦.一个集合可以有多少个元素没有限制;这意味着我的代码应该可以使用任意数量的元素.

I am unsure as to how to properly retrieve the elements out of the set in the string and put them into a Set object. I know I can parseInt once I get them by themselves but I am having trouble isolating each element. There is no limit on how many elements a set can have; which means my code should work with any number of elements.

我也考虑过正则表达式,但是我觉得似乎有一种更有效的方法.

I have also considered regular expressions but I feel as if there's a more efficient way of doing this.

任何帮助将不胜感激!

推荐答案

最简单的方法是使用Set

http://docs.oracle.com/javase/7/docs/api/java/util/HashSet.html

Arrays.asList()

http://docs.oracle.com/javase/7/docs/api/java/util/Arrays.html

将您的String[]转换为Set<String>:

Set<String> mySet = new HashSet<String>(Arrays.asList(s));

这篇关于如何解析集合的字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆