java - How can I parse a string for a set? -
java - How can I parse a string for a set? -
i writing method supposed take input string
of format "s1:{1,2,3,4}"
, set set
. set class have developed myself, , 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(){ homecoming name; }//end getname public void setname(string name){ this.name = name; }//end setname //adds object set public void add(object e) { if (size == capacity) { reallocate(); }//end if thedata[size] = (e) e; size++; (int j = 0; j<size; j++) { (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 loop }//end loop int counter = 0; (int = 0; < size; i++) { if (e == thedata[i]) { counter++; if (counter >= 2) { remove((object)e); }//end nested if }//end if }//end loop }//end add together method public e get(int i) { if (i < 0 || >= size) { throw new arrayindexoutofboundsexception(i); } else { homecoming thedata[i]; }//end else }//end method public e remove(int i) { if (i < 0 || >= size) { throw new arrayindexoutofboundsexception(i); }//end if e returnvalue = thedata[i]; (int j = + 1; j < size; j++) { thedata[j - 1] = thedata[j]; }//end loop size--; homecoming returnvalue; }//end remove method public void remove(object e) { (int = 0; < size; i++) { if (e == thedata[i]) { (int j = + 1; j < size; j++){ thedata[j - 1] = thedata[j]; }//end nested loop size--; }//end if }//end loop }//end remove method //fix! public int find(object e) { int first, last, middle; first = 0; lastly = size - 1; middle = (first+last) / 2; while(first <= lastly ) { if ((int)thedata[middle] > (int)e ) { lastly = middle - 1; } else if ((int)thedata[middle] < (int)e ) { first = middle + 1; } else { homecoming middle; }//end else }//end while if (first > last) { homecoming -1; }//end if homecoming -1; }//end find method public set<e> union(set<e> s) { set<e> returnset = new set<e>(); (int = 0; < this.size; i++) { returnset.add(this.thedata[i]); }//end loop (int = 0; < s.size; i++) { returnset.add(s.thedata[i]); }//end loop homecoming returnset; }//end union method public set<e> intersect(set<e> s) { set<e> returnset = new set<e>(); (int = 0; < this.size; i++) { (int j = 0; j < s.size; j++) { if (this.thedata[i] == s.thedata[j]){ returnset.add(thedata[i]); }//end if }//end nested loop }//end loop homecoming returnset; }//end intersect method public set<e> subtract(set<e> s) { set<e> returnset = new set<e>(); (int = 0; < this.size; i++) { (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 loop }//end loop (int = 0; < this.size; i++) { returnset.add(this.thedata[i]); }//end loop (int = 0; < s.size; i++) { returnset.add(s.thedata[i]); }//end loop homecoming returnset; }//end subtract method public boolean equals(set<e> s) { boolean result = false; (int = 0; < 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 loop homecoming 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("{"); (int = 0; < size; i++) { set.append(thedata[i]); if (i != size-1){ set.append(","); }//end if }//end loop set.append("}"); homecoming set.tostring(); }//end tostring() public setiterator<e> iterator() { setiterator<e> = new setiterator<e>() { private int currentindex = 0; public boolean hasnext() { if (currentindex < size && thedata[currentindex] != null){ currentindex++; homecoming true; } else{ homecoming false; }//end else }//end hasnext() public e next() { if (!hasnext()) { throw new nosuchelementexception(); }//end if homecoming thedata[currentindex++]; }//end next() public boolean hasprevious() { if (currentindex <= size && currentindex > 0) { currentindex--; homecoming true; } else { homecoming false; }//end else }//end hasprevious() public e previous() { if (!hasprevious()) { throw new nosuchelementexception(); }//end if homecoming thedata[currentindex--]; }//end previous() public void add(e item) { thedata[currentindex-1] = item; }//end add() public void remove() { (int = 0; < size; i++) { if (thedata[currentindex] == thedata[i]) { (int j = + 1; j < size; j++) { thedata[j - 1] = thedata[j]; }//end nested loop size--; }//end if }//end loop }//end remove() };//end new setiterator() homecoming it; }//end iterator method }//end set class
the method should
throw exception if method has invalid format such"s1:[1 2,3,4}"
(this illustration beingness missing comma , curly brace). additionally, input may have number of whitespaces , still considered valid. example: "s1: {1, 2, 3, 4 }"
. so far have method is:
public set<integer> parse(string input){ string s[] = input.split(":"); string name = s[0]; set<integer> returnset = new set<integer>(name); homecoming returnset; }
i unsure how retrieve elements out of set in string , set them set object. know can parseint
1 time them having problem isolating each element. there no limit on how many elements set can have; means code should work number of elements.
i have considered regular expressions sense if there's more efficient way of doing this.
any help appreciated!
the easiest way utilize constructor of set
http://docs.oracle.com/javase/7/docs/api/java/util/hashset.html
along arrays.aslist()
http://docs.oracle.com/javase/7/docs/api/java/util/arrays.html
to convert string[]
set<string>
:
set<string> myset = new hashset<string>(arrays.aslist(s));
java list parsing data-structures
Comments
Post a Comment