Java 平时作业三
自定义一个可以存储 int 类型的集合类(例如:SequenceList),封装一维数组存储数据,该 集合类具有以下方法:
(1)新增元素;
(2)在指定位置插入一个元素;
(3)按位置删除元素;
(4)按值删除元素
(5)排序(增序,降序)
(6)查询指定元素,返回下标位置。
1 import java.util.Scanner; 2 3 public class SequenceList { 4 5 public static void main(String[] args) { 6 int a[] = { 1,5,4,6,8,3,4,0 }; 7 int key; 8 int elem,index; 9 String c; 10 boolean flag=true; 11 Scanner sc = new Scanner(System.in); 12 IntUtil test = new IntUtil(a); 13 14 out: while(flag) { 15 System.out.println("请输入相关选项:"); 16 System.out.println("(1)新增元素"); 17 System.out.println("(2)在指定位置插入一个元素"); 18 System.out.println("(3)按位置删除元素;"); 19 System.out.println("(4)按值删除元素 "); 20 System.out.println("(5)排序(增序)"); 21 System.out.println("(6)排序(降序)"); 22 System.out.println("(7)查询指定元素,返回下标位置"); 23 System.out.println("(0)退出"); 24 key = sc.nextInt(); 25 switch(key) { 26 case 1: 27 System.out.println("请输入要添加的元素"); 28 elem=sc.nextInt(); 29 test.addElement(elem); 30 break; 31 case 2: 32 System.out.println("请输入添加的元素及位置"); 33 elem=sc.nextInt(); 34 index=sc.nextInt(); 35 test.addElementAtIndexOf(elem, index); 36 break; 37 case 3: 38 System.out.println("请输入要删除元素的位置"); 39 index=sc.nextInt(); 40 test.deleteElementAtIndexOf(index); 41 break; 42 case 4: 43 System.out.println("请输入要删除的元素的值"); 44 elem=sc.nextInt(); 45 test.deleteElement(elem); 46 break; 47 case 5: 48 test.arraySortUp(); 49 break; 50 case 6: 51 test.arraySortDown(); 52 break; 53 case 7: 54 System.out.println("请输入要查找的元素"); 55 elem=sc.nextInt(); 56 test.searchElement(elem); 57 break; 58 case 0: 59 flag=false; 60 System.out.println("程序已结束"); 61 break out; 62 default: 63 System.out.println("没有该选项,请重新输入"); 64 continue; 65 } 66 System.out.println("是否继续?y/n"); 67 c=sc.next(); 68 if(c.equals("n")) 69 { flag=false; 70 System.out.println("程序已结束"); 71 } 72 if(c.equals("y")) 73 flag=true; 74 } 75 76 } 77 78 } 79 80 81 class IntUtil { 82 int array[]; 83 84 public IntUtil(int a[]) { 85 super(); 86 array = a; 87 } 88 89 public void arraySortDown() { 90 // TODO Auto-generated method stub 91 92 for (int i = 0; i < array.length; i++) { 93 for (int j = i + 1; j < array.length; j++) { 94 if (array[i] < array[j]) { 95 int temp = array[i]; 96 array[i] = array[j]; 97 array[j] = temp; 98 } 99 } 100 } 101 show(); 102 } 103 104 public void arraySortUp() { 105 // TODO Auto-generated method stub 106 for (int i = 0; i < array.length; i++) { 107 for (int j = i + 1; j < array.length; j++) { 108 if (array[i] > array[j]) { 109 int temp = array[i]; 110 array[i] = array[j]; 111 array[j] = temp; 112 } 113 } 114 } 115 show(); 116 } 117 118 void addElement(int a) { 119 int[] newArray = new int[array.length + 1]; 120 121 for (int i = 0; i < array.length; i++) { 122 newArray[i] = array[i]; 123 } 124 125 newArray[array.length] = a; 126 127 array = newArray; 128 129 show(); 130 } 131 132 void addElementAtIndexOf(int a, int index) { 133 index=index-1; 134 int[] newArray = new int[array.length + 1]; 135 136 for (int i = 0; i < index; i++) { 137 newArray[i] = array[i]; 138 } 139 140 newArray[index] = a; 141 142 for (int i = index + 1; i < array.length; i++) { 143 newArray[i] = array[i - 1]; 144 } 145 newArray[array.length] = array[array.length - 1]; 146 array = newArray; 147 show(); 148 } 149 150 void deleteElementAtIndexOf(int index) { 151 int[] newArray = new int[array.length - 1]; 152 153 for (int i = 0; i < index - 1; i++) { 154 newArray[i] = array[i]; 155 } 156 157 for (int i = index; i < array.length; i++) { 158 newArray[i - 1] = array[i]; 159 } 160 array = newArray; 161 show(); 162 } 163 164 void deleteElement(int a) { 165 for (int i = 0; i < array.length; i++) { 166 if (a == array[i]) { 167 deleteElementAtIndexOf(i + 1); 168 } 169 } 170 171 } 172 173 int elementAt(int a) { 174 return 0; 175 } 176 177 int searchElement(int a) { 178 for(int i=0;i<array.length;i++) 179 { 180 if(array[i]==a) 181 { System.out.println("该数的下标为"+i); 182 return 0; 183 } 184 185 } 186 System.out.print("该数不存在"); 187 return 0; 188 } 189 190 void show() { 191 for (int i = 0; i < array.length; i++) { 192 System.out.print(array[i]+" "); 193 } 194 System.out.println(); 195 } 196 }