java编程,急急急!!!!!!!! 给一个数组给你,你给它的所有奇数用升序,给它的所有偶数用来降序,最后给偶数放在前面奇数放在后面? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 public static void main(String[] args) { int[] arr={1,2,3,4,5,6,7,8,9,10,11,12,13,14}; List<Integer> odd=new ArrayList<Integer>(); List<Integer> even=new ArrayList<Integer>(); List<Integer> list=new ArrayList<Integer>(); for(int i:arr){ if((i&1)==1) odd.add(i); else even.add(i); } Collections.sort(even,new Comparator<Integer>(){ @Override public int compare(Integer o1, Integer o2) { return o1>o2?-1:((o1<o2)?1:0); } }); Collections.sort(odd); list.addAll(even); list.addAll(odd); System.out.println(list); } 比如说:Array = {7,12,34,105,37,58,5,43,25,46,80,92}最后是:92,80,58,46,34,12,5,7,25,37,43,105这种效果 public class Test19 { public int[] sort(int[] a) { int[] array = new int[a.length]; int tail=a.length-1; int head=0; for(int i=0; i<a.length; i++) { for(int j=i+1; j<a.length; j++) { if(a[i]>a[j]) { int tmp = a[i]; a[i] = a[j]; a[j] = tmp; } } } //从小到大排序 for(int x=a.length-1; x>=0; x--) { if(a[x]%2==1) { array[tail]=a[x]; tail--; } if(a[x]%2==0) { array[head]=a[x]; head++; } } //对奇偶进行排序 return array; } public static void main(String args[]) { Test19 t19 = new Test19(); int[] a = {1,8,4,2,6,0,5,3}; //测试数组 int[] b = t19.sort(a); for(int i=0; i<b.length; i++) { System.out.print(b[i] + " "); } }} http://atomti.javaeye.com/blog/575625 去看下java的jdk中的算法....不就知道啦 public static void main(String[] args) { Integer[] arr={1,2,3,4,5,6,7,8,9,10,11,12,13,14}; //List<Integer> odd=new ArrayList<Integer>(); //List<Integer> even=new ArrayList<Integer>(); //List<Integer> list=new ArrayList<Integer>(); //for(int i:arr){ // if((i&1)==1) // odd.add(i); // else // even.add(i); //} //Collections.sort(even,new Comparator<Integer>(){ // @Override // public int compare(Integer o1, Integer o2) { // return o1>o2?-1:((o1<o2)?1:0); // } //}); Arrays.sort(arr, new Comparator<Integer>(){ public int compare(Integer o1, Integer o2){ if(o1.intValue() %2 == 0 && o2.intValue() %2 == 0){ return -o1.compareTo(o2); } else if(o1.intValue() %2 != 0 && o2.intValue() %2 != 0){ return o1.compareTo(o2); } else{ return o1.intValue() % 2 == 0 ? -1 : 1; } } }); //Collections.sort(odd); //list.addAll(even); //list.addAll(odd); System.out.println(Arrays.toString(arr)); } package cn.edu.qtc;public class ArraySort { //遍历输出数组 public static void print(int a[]){ for(int i=0;i<a.length;i++){ System.out.print(a[i]+","); } } //倒序输出 public static void print2(int a[]){ for(int i=a.length-1;i>=0;i--){ System.out.print(a[i]+","); } } //冒泡排序 public static void sort(int a[]){ int len=a.length; for(int i=0;i<len;i++) { for(int j=i+1;j<len;j++) { int temp; if(a[i]>a[j]) { temp=a[j]; a[j]=a[i]; a[i]=temp; } } } } public static void main(String[] args) { // ArraySort as=new ArraySort(); int [] a={7,12,34,105,37,58,5,43,25,46,80,92}; int count=0; for(int i=0;i<a.length;i++){ if(a[i]%2==0){ count++; } } int b[]=new int[count]; int c[]=new int[a.length-count]; int j=0,k=0; //把偶数放入数组b,奇数放入数组 c for(int i=0;i<a.length;i++){ if(a[i]%2==0){ b[j++]=a[i]; }else{ c[k++]=a[i]; } } as.sort(b); as.print2(b); as.sort(c); as.print(c); }} servlet问题 报表 找找! 如何实现邮件客户端,基于WEB的? 关于HIBERNATE的load问题~~ 大家好,想向大家讨教一个关于SourceOffSite的配置问题. 基础问题,关于持久层的疑惑 初学struts 遇到问题 WEBLOGIC8。11+SQL SERVER配置问题,郁闷了两天了,看了所有的CSDN帖子都没有解决。。 LINUX环境下java执行static代码快报NoClassDefFoundError 求weblogic 8.1 org.apache.commons.net.ftp.FTPClient 上传的问题。急!!!
public static void main(String[] args) {
int[] arr={1,2,3,4,5,6,7,8,9,10,11,12,13,14};
List<Integer> odd=new ArrayList<Integer>();
List<Integer> even=new ArrayList<Integer>();
List<Integer> list=new ArrayList<Integer>();
for(int i:arr){
if((i&1)==1)
odd.add(i);
else
even.add(i);
}
Collections.sort(even,new Comparator<Integer>(){
@Override
public int compare(Integer o1, Integer o2) {
return o1>o2?-1:((o1<o2)?1:0);
}
});
Collections.sort(odd);
list.addAll(even);
list.addAll(odd);
System.out.println(list);
}
最后是:92,80,58,46,34,12,5,7,25,37,43,105这种效果
public int[] sort(int[] a) {
int[] array = new int[a.length];
int tail=a.length-1;
int head=0;
for(int i=0; i<a.length; i++) {
for(int j=i+1; j<a.length; j++) {
if(a[i]>a[j]) {
int tmp = a[i];
a[i] = a[j];
a[j] = tmp;
}
}
} //从小到大排序
for(int x=a.length-1; x>=0; x--) {
if(a[x]%2==1) {
array[tail]=a[x];
tail--;
}
if(a[x]%2==0) {
array[head]=a[x];
head++;
}
} //对奇偶进行排序
return array;
}
public static void main(String args[]) {
Test19 t19 = new Test19();
int[] a = {1,8,4,2,6,0,5,3}; //测试数组
int[] b = t19.sort(a);
for(int i=0; i<b.length; i++) {
System.out.print(b[i] + " ");
} }
}
Integer[] arr={1,2,3,4,5,6,7,8,9,10,11,12,13,14};
//List<Integer> odd=new ArrayList<Integer>();
//List<Integer> even=new ArrayList<Integer>();
//List<Integer> list=new ArrayList<Integer>();
//for(int i:arr){
// if((i&1)==1)
// odd.add(i);
// else
// even.add(i);
//}
//Collections.sort(even,new Comparator<Integer>(){
// @Override
// public int compare(Integer o1, Integer o2) {
// return o1>o2?-1:((o1<o2)?1:0);
// }
//});
Arrays.sort(arr, new Comparator<Integer>(){
public int compare(Integer o1, Integer o2){
if(o1.intValue() %2 == 0 && o2.intValue() %2 == 0){
return -o1.compareTo(o2);
}
else if(o1.intValue() %2 != 0 && o2.intValue() %2 != 0){
return o1.compareTo(o2);
}
else{
return o1.intValue() % 2 == 0 ? -1 : 1;
}
}
});
//Collections.sort(odd);
//list.addAll(even);
//list.addAll(odd);
System.out.println(Arrays.toString(arr));
}
public static void print(int a[]){
for(int i=0;i<a.length;i++){
System.out.print(a[i]+",");
}
}
//倒序输出
public static void print2(int a[]){
for(int i=a.length-1;i>=0;i--){
System.out.print(a[i]+",");
}
}
//冒泡排序
public static void sort(int a[]){
int len=a.length;
for(int i=0;i<len;i++)
{
for(int j=i+1;j<len;j++)
{
int temp;
if(a[i]>a[j])
{
temp=a[j];
a[j]=a[i];
a[i]=temp;
}
}
}
}
public static void main(String[] args) {
//
ArraySort as=new ArraySort();
int [] a={7,12,34,105,37,58,5,43,25,46,80,92};
int count=0;
for(int i=0;i<a.length;i++){
if(a[i]%2==0){
count++;
}
}
int b[]=new int[count];
int c[]=new int[a.length-count];
int j=0,k=0;
//把偶数放入数组b,奇数放入数组 c
for(int i=0;i<a.length;i++){
if(a[i]%2==0){
b[j++]=a[i];
}else{
c[k++]=a[i];
}
}
as.sort(b);
as.print2(b);
as.sort(c);
as.print(c);
}
}