直接調(diào)用jdk里面的方法Arrays.sort(args)方法。而且這個(gè)方法重載實(shí)現(xiàn)了多個(gè)參數(shù),排序下標(biāo)[N~M]位數(shù)字~倒敘,升序等等~
玉樹網(wǎng)站建設(shè)公司成都創(chuàng)新互聯(lián)公司,玉樹網(wǎng)站設(shè)計(jì)制作,有大型網(wǎng)站制作公司豐富經(jīng)驗(yàn)。已為玉樹上千多家提供企業(yè)網(wǎng)站建設(shè)服務(wù)。企業(yè)網(wǎng)站搭建\外貿(mào)網(wǎng)站制作要多少錢,請找那個(gè)售后服務(wù)好的玉樹做網(wǎng)站的公司定做!
for (int i = 0; i list.size(); i++) {
for (int j = i + 1; j list.size(); j++) {
if (list.get(i) list.get(j)) {
int temp = list.get(i);
list.set(i, list.get(j));
list.set(j, temp);
}
}
}
System.out.println("升序:" + list);
for (int i = 0; i list.size(); i++) {
for (int j = i + 1; j list.size(); j++) {
if (list.get(i) list.get(j)) {
int temp = list.get(i);
list.set(i, list.get(j));
list.set(j, temp);
}
}
}
System.out.println("降序:" + list);
java倒序輸出:
public?static?void?main(String[]?args)
{
String?a?=?"abcde";
/**?從最后一個(gè)字符開始,倒敘一個(gè)字母一個(gè)字母的輸出?*/
for?(int?i?=?a.length()?-?1;?i?=?0;?i--)
{
System.out.print(a.charAt(i));
}
}
JS獲取復(fù)選框的值(必須在jsp頁面引入jQuery,不懂可以追問)
script?type="text/javascript"?language="javascript"
function?showMsg()
{
var?str="";
$(":checkbox").each(function(){
if?($(this).is(':checked'))
{
var?value?=?$(this).attr("value");
str?=?str?+?value?+",";?
}?
});
alert("你選中的是:"+str);
}
/script
input?type="checkbox"?name="info"?value="A"/Abr/
input?type="checkbox"?name="info"?value="B"/Bbr/
input?type="checkbox"?name="info"?value="C"/Cbr/
input?type="checkbox"?name="info"?value="D"/Dbr/
input?type="checkbox"?name="info"?value="E"/E
br/br/
input?type="button"?value="點(diǎn)我啊"?onclick="showMsg()"/
1、逆序數(shù)組有很多種方法,比如先排序,再逆序存
public static void main(String[] args) {
int[] nums = {1, 3, 5, 7, 9, 11, 13, 15, 17, 19};
System.out.print("原數(shù)組:");
for (int num : nums) {
System.out.print(num+" ");
}
System.out.print("\n倒序新數(shù)組:");
for (int num : reverseArray1(nums)) {
System.out.print(num + " ");
}
}
//1.排序后倒序
public static int[] reverseArray1(int[] nums) {
Arrays.sort(nums);
int[] reNums = new int[nums.length];
for (int i = 0; i nums.length; i++) {
reNums[i] = nums[nums.length - 1 - i];
}
return reNums;
}
//2.Collection 內(nèi)置的逆序
public static int[] reverseArray2(int[] nums) {
ArrayListInteger list = new ArrayList();
for (int i = 0; i nums.length; i++) {
list.add(nums[i]);
}
Collections.reverse(list);
int [] reNums = new int[nums.length];
for (int i = 0; i nums.length ; i++) {
reNums[i] = list.get(i);
}
return ?reNums;
}
java中用arrays sort()方法將abcdefg倒序,可以先試用java的comparable接口先進(jìn)行比較,然后排序,實(shí)例如下:
package?test;
import?java.util.Arrays;
import?java.util.Comparator;
public?class?Arraysort?{
public?static?void?main(String[]?args)?{
String?s?=?"a,b,c,d,e,f,g";
String[]?strArr?=?s.split(",");
Arrays.sort(strArr,?new?com());//排序
for(String?str?:?strArr){
System.out.print(str+",");
}
}
}
class?comT?implements?ComparatorT?{//對abcdefg進(jìn)行比較大小,排序
public?int?compare(T?o1,?T?o2)?{
int?i?=?Integer.parseInt(String.valueOf(o1));
int?j?=?Integer.parseInt(String.valueOf(o2));
if?(i??j)?return?1;
if?(i??j)?return?-1;
return?0;
}
}
方法一:實(shí)現(xiàn)Comparable接口排序package collsort.comparable;
package com.cvicse.sort.comparable;
public class Cat implements ComparableCat {
private int age;
private String name;
public Cat(int age, String name) {
this.age = age;
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
......
public int compareTo(Cat o) {
return this.getAge() - o.getAge();
}
......
}
通過實(shí)現(xiàn)Comparable接口實(shí)現(xiàn)個(gè)性化排序測試。排序測試,Collection.sort(list)升序排列Collections.sort(list, Collections.reverseOrder());降序排列;Collections.reverse(list);反轉(zhuǎn)排序,先輸出列表最后一個(gè)元素
public class TestComparable {
public static void main(String args[]) {
test();
test2();
}
public static void test() {
......
ListCat listCat1 = new ArrayListCat();
Cat cat1 = new Cat(34, "hehe");
Cat cat2 = new Cat(12, "haha");
Cat cat3 = new Cat(23, "leizhimin");
Cat cat4 = new Cat(13, "lavasoft");
listCat1.add(cat1);
listCat1.add(cat2);
listCat1.add(cat3);
......
System.out.println("調(diào)用Collections.sort(ListT list)listCat2升序排序:");
Collections.sort(listCat1);
System.out.println("降序排列元素:");
Collections.sort(listCat1, Collections.reverseOrder());
System.out.println("Collections.reverse 從列表中最后一個(gè)元素開始輸出:");
Collections.reverse(listCat1);
......
}
/**
* 針對數(shù)組的排序
*/
public static void test2() {
String[] strArray = new String[] { "z", "a", "C" };
System.out.println("數(shù)組轉(zhuǎn)換為列表");
ListString list = Arrays.asList(strArray);
System.out.println("順序排序列表");
Collections.sort(list);
System.out
.println("按String實(shí)現(xiàn)的Comparator對象String.CASE_INSENSITIVE_ORDER排序----");
Collections.sort(list, String.CASE_INSENSITIVE_ORDER);
System.out.println("倒序排序列表");
Collections.sort(list, Collections.reverseOrder());
......
}
}
方法二:實(shí)現(xiàn)Comparator接口排序
public class Person {
private int age;
private String name;
......
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
......
}
實(shí)現(xiàn)了Comparator接口,重寫了compare方法
import java.util.Comparator;
public class PersonComparator implements ComparatorPerson {
public int compare(Person o1, Person o2) {
return o1.getAge() - o2.getAge();
}
}
測試方法
public class TestComparator {
public static void main(String args[]) {
test1();
}
public static void test1() {
System.out.println("升序排序測試:");
ListPerson listPerson = new ArrayListPerson();
Person person1 = new Person(34, "lavasoft");
Person person2 = new Person(12, "lavasoft");
Person person3 = new Person(23, "leizhimin");
Person person4 = new Person(13, "sdg");
listPerson.add(person1);
listPerson.add(person2);
listPerson.add(person3);
ComparatorPerson ascComparator = new PersonComparator();
System.out.println("排序后集合為:");
// 利用Collections類靜態(tài)工具方法對集合List進(jìn)行排序
Collections.sort(listPerson, ascComparator);
System.out.println("\n降序排序測試:");
// 從升序排序?qū)ο螽a(chǎn)生一個(gè)反轉(zhuǎn)(降序)的排序?qū)ο?/p>
ComparatorPerson descComparator = Collections
.reverseOrder(ascComparator);
System.out.println("利用反轉(zhuǎn)后的排序接口對象對集合List排序并輸出:");
Collections.sort(listPerson, descComparator);
outCollection(listPerson);
}
}