编程论坛's Archiver

taoshucheng 发表于 2008-4-6 09:42

请求帮助,数组问题,初学者

两个一维数组合并成一个一维数组,并且按数值的大小,从大到小排列
public class xiti6_11
{
    public static void main(String args[])
    {
        int[] A={19,2,6,8,9};//声明三个数组
        int[] B={1,20,7,15,4};
        int[] C=new int[A.length+B.length];//定义一个数组用来存放两个数组
        //复制
        System.arraycopy(A, 0, C, 0, 5);
        System.arraycopy(B, 0, C, 5, 5);
        //排序
        for(int i=0;i<C.length-1;i++)
        {
            if(C[i]<C[i+1])
                {
                    int temp=C[i];
                    C[i]=C[i+1];
                    C[i+1]=temp;       
                }
        }       
        for(int j=0;j<C.length;j++)
        System.out.print(C[j]+"\t");          
     }
}
编译的时候有问题,我想应该是排序的时候错了,请大家帮忙提出

sunkaidong 发表于 2008-4-6 11:16

public class xiti6_11
{
    public static void main(String args[])
    {
    int[] A={19,2,6,8,9};//声明三个数组
    int[] B={1,20,7,15,4};
    int[] C=new int[A.length+B.length];//定义一个数组用来存放两个数组
    //复制
    System.arraycopy(A, 0, C, 0, 5);
    System.arraycopy(B, 0, C, 5, 5);
    //排序
        for(int i=0;i<C.length-1;i++)
        {
        for(int j=0;j<C.length-1;j++)
        if(C[j]<C[j+1])
                {
                    int temp=C[j];
                    C[j]=C[j+1];
                    C[j+1]=temp;   
            }
        }   
    for(int j=0;j<C.length;j++)
    System.out.print(C[j]+"\t");      
     }
}

taoshucheng 发表于 2008-4-10 10:13

谢谢,十分感谢,我是数组输出的时候没有弄好,

页: [1]

Powered by Discuz! Archiver 6.1.0  © 2001-2007 Comsenz Inc.