- 论坛徽章:
- 0
|
本帖最后由 so_brave 于 2011-10-26 16:00 编辑
Java从数据中删除指定元素
Java代码- 1./**
- 2. * 已知一组有序数列,输入任意数,从这个数列中删除这个数, 显示删除后数列
- 3. */
- 4.public static void main(String[] args) {
- 5. int num[] = new int[] { 1, 2, 3, 4, 5, 6, 7 };
- 6. int m = 7;
- 7. int[] copy = new int[num.length-1];
- 8. int j = 0;
- 9. for (int i = 0; i < num.length; i++) {
- 10. if (m == num[i]) {
- 11. i = i + 1;
- 12. }
- 13. if(i<num.length){
- 14. copy[j]=num[i];
- 15. }
- 16. j++;
- 17. }
- 18. for (int i = 0; i < copy.length; i++) {
- 19. System.out.print(copy[i]+" ");
- 20. }
- 21.}
复制代码 |
|