Tuesday 15 June 2010

java - finding closet value in another list -



java - finding closet value in another list -

please allow me know if question not clear. can provide more info. have next musical notes in arraylist

arraylist<string> noteslist = new arraylist<>(); noteslist.add("a"); //0 noteslist.add("a#"); //1 noteslist.add("b"); //2 noteslist.add("c"); //3 noteslist.add("c#"); //4 noteslist.add("d"); //5 noteslist.add("d#"); //6 noteslist.add("e"); //7 noteslist.add("f"); //8 noteslist.add("f#"); //9 noteslist.add("g"); //10 noteslist.add("g#"); //11

i have 2 sub lists, 1 ascending , 1 descending. both of them contain notes part of noteslist.

arraylist<string> ascendlist = new arraylist<>(); arraylist<string> descendlist = new arraylist<>();

ex:

ascendlist = c, d, e, f#, g //indices in noteslist = 3,5,7,9,10 descendlist = a, c#, e, f, g, g# // indices in noteslist = 0,4,7,8,10,11

i need generate patterns going through ascending list , descending vice versa. example:

d, e (from ascending list), e, c# (from descending list) c,d,e,f#

(ascend), f,e,c#,a (descend)

f e c# (descend), d, e (ascend)

there status doing this. when want connect descending pattern @ end of ascending pattern, need find next note equal or below lastly ascending note, in descending list.

ex: if f# lastly note in 1 of pattern of ascending type, f should first note in descending pattern. if e lastly note in ascending pattern, 1 time again e first note in descending pattern since e nowadays in both ascend , descend. when generate ascending pattern @ end of descending pattern, need find next note equal or higher lastly descending note.

i not figure out how this. 1 method tried go indices both ascending , descending corresponding noteslist , thought of mapping , using map.higherkey, map.lowerkey, etc methods. unable proceed because not map them.

enum note { c, c_, d, d_, e, f, f_, g, g_, a, a_, b; public note above() { homecoming ordinal() != values().length - 1 ? note.values()[ordinal() + 1] : note.values()[0]; } public note below() { homecoming ordinal() > 0 ? note.values()[ordinal() - 1] : note.values()[note.values().length - 1]; } } interface scale { listiterator<note> joinafter(note note); } scale ascending = new scale() { list<note> notes = aslist(c, d, e, f_, g); @override public listiterator<note> joinafter(note note) { homecoming notes.listiterator(notes.contains(note)? notes.indexof(note) : notes.indexof(note.above())); } }; scale descending = new scale() { list<note> notes = aslist(a, c_, e, f, g, g_); @override public listiterator<note> joinafter(note note) { homecoming notes.listiterator(notes.contains(note)? notes.indexof(note) : notes.indexof(note.below())); } };

i'd recomend add together own iterator wrapper phone call previous next in descendingscale (if don't want reason reverse notes there) , more importantly allow infinitely loop through scales.

if school asignment improve stick numbers , don't mess lists of strings untill need print things out. btw array improve selection here.

java

No comments:

Post a Comment