2 little programe little_programe 于 2015.06.07 21:51 提问

java的入门的数组问题,求搭救

删除{20,90,50,30,10}数组中的指定数字,并保持位置连续

2个回答

danielinbiti
danielinbiti   Ds   Rxr 2015.06.07 21:52
已采纳
 public class Test {

private int a[] = {20,90,50,30,10};//数组初始值1 2 3 4 5

public void delete(int n)//删除数组中n的值
{
for (int i = 0; i < a.length; i++) {
if(a[i] == n)
{
for(int j = i; j < a.length-1; j++)
{
a[j] = a[j+1];
}
}
}
}
public void print()//打印数组
{
for (int i = 0; i < a.length-1; i++) {
System.out.println (a[i]);
}
}

    public static void main(String[] args) {
Test t = new Test();
t.delete(30);
t.print();
    }
}
cuiwei1026522829
cuiwei1026522829   Ds   Rxr 2015.06.07 22:16

public class C {

public static void main(String[] args) {
    int[] ary = {20,10,50,40,30,70,60,80,90,100};

    sort(ary);
    System.out.println("After sort, array item is: ");
    for(int i = 0; i < ary.length; i++){
        System.out.print(ary[i] + " ");
    }

}

private static void sort(int[] ary) {

    for(int i = 0; i < ary.length - 1; i++){

        for(int j = i + 1; j < ary.length; j++){
            if(ary[j] > ary[i]){
                int temp = ary[i];
                ary[i] = ary[j];
                ary[j] = temp;
            }
        }

    }
}

}

------------------testing
After sort, array item is:
100 90 80 70 60 50 40 30 20 10

Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!