• 博客园logo
  • 会员
  • 周边
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
卡苏米
博客园    首页    新随笔    联系   管理    订阅  订阅

数组算法小题一

package shuzu_practice;

public class Practice1 {
/*
需求1:
数组去重:
原数组:{1,9,8,6,3,5,1,9,8,7,2,6,6}
去重后数组:{1,9,8,6,3,5,7,2}
不允许使用集合
*/
public static void main(String[] args) {

int[] oldArr = {1, 9, 8, 6, 3, 5, 1, 9, 8, 7, 2, 6, 6};
int[] newArr = new int[oldArr.length];

newArr[0] = oldArr[0];
int index = 1;//定义新数组的长度
for (int i = 1; i < oldArr.length; i++) {
boolean l = false;
for (int j = 0; j < index; j++) {
if (oldArr[i] == newArr[j]) {
l = true;
}
}
if(!l){
newArr[index] = oldArr[i];
index++;
}
}
for (int i = 0; i < index; i++) {
System.out.println(newArr[i]);
}
}
}
posted @ 2021-08-14 10:42  卡苏米  阅读(65)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2026
浙公网安备 33010602011771号 浙ICP备2021040463号-3