java 平时作业三

 

自定义一个可以存储int类型的集合类(例如:SequenceList),封装一维数组存储数据,该集合类具有以下方法:
(1)新增元素;
(2)在指定位置插入一个元素;
(3)按位置删除元素;
(4)按值删除元素
(5)排序(增序,降序)
(6)查询指定元素,返回下标位置。

 

  1 package hi;
  2 
  3 public class SequenceList {
  4 
  5     /**
  6      * @param args
  7      */
  8     // Int类型的集合类
  9         int array[]= { 0,1,2,3,4,5,6,7,8,9 };
 10         
 11         
 12         void show() {
 13             for (int i = 0; i < array.length; i++) {
 14                 System.out.print(array[i]+"  ");
 15             }
 16             System.out.println("");
 17         }
 18         
 19         void addElement(int a){
 20             int [] newArray = new int[array.length+1];
 21             
 22             for (int i = 0;i<array.length;i++){
 23                 newArray[i] = array[i];
 24             }
 25             newArray[array.length] = a;
 26             array = newArray;
 27             show();
 28         }
 29         void addElementAtIndexof(int a,int index){
 30             index--;
 31             int[] newArray = new int[array.length+1];
 32             
 33             for (int i=0;i<index;i++)
 34             {
 35                 newArray[i] = array[i];
 36             }
 37             
 38             newArray [index] = a;
 39             
 40             for (int i = index + 1; i <= array.length; i++) {
 41                 newArray[i] = array[i - 1];
 42             }
 43             array = newArray;
 44             show();
 45         }
 46         void deleteElementAtIndexOf(int index) {
 47             int[] newArray = new int[array.length - 1];
 48 
 49             for (int i = 0; i < index - 1; i++) {
 50                 newArray[i] = array[i];
 51             }
 52 
 53             for (int i = index; i < array.length; i++) {
 54                 newArray[i - 1] = array[i];
 55             }
 56             array = newArray;
 57             show();
 58         }
 59         void deleteElement(int a) {
 60             for (int i = 0; i < array.length; i++) {
 61                 if (a == array[i]) {
 62                     deleteElementAtIndexOf(i + 1);
 63                 }
 64             }
 65 
 66         }
 67         int elementAt(int a) {
 68             return 0;
 69         }
 70         public void arraySortDown() {
 71 
 72             for (int i = 0; i < array.length; i++) {
 73                 for (int j = i + 1; j < array.length; j++) {
 74                     if (array[i] < array[j]) {
 75                         int temp = array[i];
 76                         array[i] = array[j];
 77                         array[j] = temp;
 78                     }
 79                 }
 80             }
 81             show();
 82         }
 83 
 84         public void arraySortUp() {
 85             
 86             for (int i = 0; i < array.length; i++) {
 87                 for (int j = i + 1; j < array.length; j++) {
 88                     if (array[i] > array[j]) {
 89                         int temp = array[i];
 90                         array[i] = array[j];
 91                         array[j] = temp;
 92                     }
 93                 }
 94             }
 95             show();
 96         }
 97         public void arraySortIndexof(int a)
 98         {
 99             int index2=0;
100             for (int i = 0; i < array.length; i++) {
101                 if (a == array[i]) {
102                     index2=i;
103                     break;
104                 }
105             }
106             System.out.println(index2);
107         }
108     
109 
110     public static void main(String[] args) {
111        SequenceList test = new SequenceList();
112        test.addElement(10);
113        test.addElementAtIndexof(9, 1);
114        test.deleteElementAtIndexOf(3);
115        test.deleteElement(3);
116        test.arraySortUp();
117        test.arraySortDown();
118        test.arraySortIndexof(4);
119     }
120 
121 }

 

posted @ 2019-03-21 16:18  CheeseIce  阅读(270)  评论(0编辑  收藏  举报