删除{20,90,50,30,10}数组中的指定数字,并保持位置连续
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();
}
}
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