IT 森林@IT·互联网

数组_算法2

2017-04-27  本文已影响13人  养码哥
 `package com.ithelei.cn;

 public class ArrayApp {
 public static void main(String[] args) {

    LowArray arr;
    arr = new LowArray(100);
    int nElems;// 元素个数
    int j;// 循环
    long searchKey;// 查找的数据项

    arr.setElem(0, 77);// 第一个参数是位置
    arr.setElem(1, 99);
    arr.setElem(2, 44);
    arr.setElem(3, 55);
    arr.setElem(4, 22);
    arr.setElem(5, 88);
    arr.setElem(6, 11);
    arr.setElem(7, 00);
    arr.setElem(8, 66);
    arr.setElem(9, 33);

    nElems = 10;
    // 显示所有元素-------------
    for (j = 0; j < nElems; j++)
    System.out.print(arr.getElem(j) + " ");
    System.out.println();

    // 查找26
    searchKey = 26;
    for (j = 0; j < nElems; j++)
    if (arr.getElem(j) == searchKey)break;
        
    if (j == nElems)
        System.out.println("没有找到" + searchKey);
    else
        System.out.println("找到了" + searchKey);

    // 删除55
    searchKey = 55;
    for (j = 0; j < nElems; j++)
    if (arr.getElem(j) == searchKey)break;

    for (int k = j; k < nElems; k++)
    arr.setElem(k, arr.getElem(k + 1));
    nElems--;

    // 显示所有元素
    for (j = 0; j < nElems; j++)
    System.out.print(arr.getElem(j) + " ");
    System.out.println();
}

}
`

`package com.ithelei.cn;

public class HighArrayApp {
public static void main(String[] args) {
    int maxSiz=100;
    HighArray arr;
    arr=new HighArray(maxSiz);
    arr.insert(77);
    arr.insert(99);
    arr.insert(44);
    arr.insert(55);
    arr.insert(22);
    arr.insert(88);
    arr.insert(11);
    arr.insert(00);
    arr.insert(66);
    arr.insert(33);
    
    arr.display();
    int searchKey=35;
    if(arr.find(searchKey))
        System.out.println("找到了"+searchKey);
    else
        System.out.println("没有找到"+searchKey);
    
    arr.delete(00);
    arr.delete(55);
    arr.delete(99);
    
    arr.display();
} 

}
`
上一篇下一篇

猜你喜欢

热点阅读