我在我的班級中有一個Vector,我聲明爲Vector v = new Vector();
。當我在控制檯中打印矢量時,我得到的值爲[120, 160, 170, 180, 190, 210, 80]
。現在我需要按升序對這個向量進行排序。在java中排列矢量
我該怎麼做?任何人都可以幫助我做到這一點..
在此先感謝。 Sundeep.S
我在我的班級中有一個Vector,我聲明爲Vector v = new Vector();
。當我在控制檯中打印矢量時,我得到的值爲[120, 160, 170, 180, 190, 210, 80]
。現在我需要按升序對這個向量進行排序。在java中排列矢量
我該怎麼做?任何人都可以幫助我做到這一點..
在此先感謝。 Sundeep.S
您可以使用Collections#sort()
對向量排序。它會按升序排序。請查看javadoc瞭解更多詳情。
你必須使用Collections.sort()
方法:
Collections.sort(v);
Collections.sort(v)
將做到這一點。
使用:java.util.Collections.sort()
這將滿足您的要求:
List v = new Vector(); //rather use List instead of Vector (more elegant style)
//adding of values here
Colletions.sort(v);
System.out.println(v.toString());
使用'Collections.sort(V)'。也不要使用原始類型。 'Vector v =新載體();' –
認真......? – markovuksanovic