i have array list filled objects each have info them. example :
{term(c,2), term(h,2), term(c,1), term(o,3)}
the goal right change arraylist result such :
{term(c,3), term(h,2), term(o,3)}
we want collate terms together. have following @ stage.
char element = 'a'; int amt_atom = 0; int index = 0; char alphabet = 'a'; char alpha = 'a'; (; alpha <= 'z' ; alpha++){ search: ( ; alphabet <= 'z' ; alphabet++ ) { ( int = 0 ; < terms.size() ; i++){ if ( terms.get(i).getelement() == alphabet){ element = terms.get(i).getelement(); amt_atom = terms.get(i).getatoms(); index = i; system.out.println(element + " , " + amt_atom + " , " + index ); alphabet++; break search; } } } (int = 0 ; < terms.size() ; i++){ if ( terms.get(i).getelement() == element && != index){ int amt_atoms2 = terms.get(i).getatoms(); terms.set(index, new term ( element, amt_atom + amt_atoms2 )); terms.remove(i); } } }
also, array given might not alphabetic , must put in alphabetic order. example be: ch3ch2ch2ch2ch2ch3
, result = c6h14
.
edit : sorry, should have specified wrong original code. so, doing c2h2ho3
results in c2h3o3
which fantastic because right when try ch3ch2ch2ch2ch2ch3
c2h5
not @ correct. not sure why .
am on right track? maybe tell me doing wrong? new java , therefore have extremely basic knowledge if can explain while using structures , elements of java might have used above. thanks
how first sort, collate? make term
's implement comparable
interface , call terms.sort();
. go on list , collate, easier once it's sorted.
don't worry performance here. should fast.
Comments
Post a Comment