java - Giving Position to values in an ArrayList -
i'm working on assigning positions values in arraylist in java.
for example arraylist containing {5,6,3,1,9,10,0,2,6}
.
how can correctly assign positions numbers have {5th,3rd,7th 2nd,1st,8th, 6th,3rd}
.
i have sorted arraylist containing numbers in java.
am stuck @ giving position numbers.
so work?
public static arraylist<string> getpositions(arraylist<integer> list) { arraylist<string> positions = new arraylist<string>(); string[] endings = { "th", "st", "nd", "rd" }; integer last = 100; integer lastrank = 1; (int index = 0; index < list.size(); index++) { int position = index + 1; if (last == list.get(index)) { position = lastrank; } int remainder = position % 10; string rank = position + ""; string ending = endings[0]; if (remainder <= 3) { if ((position % 100) - remainder != 10) { ending = endings[remainder]; } } rank = rank + ending; last = list.get(index); lastrank = position; positions.add(rank); } return positions; }
i give credit robert van der spek coming more efficient way assign ending of position.
Comments
Post a Comment