删除数组中重复的数据(不准用集合、框架)
2016-06-15 15:09
295 查看
使用集合框架比较简单,只需要用元素不能重复的比如set就行。
public class Ddq {
public static void main(String[] args) {
int arr[] = {3, 3, 3, 7, 7, 7, 9, 8, 2, 2, 7, 7, 3, 3, 0, 8};
String str = "";
for (int i = 0; i < arr.length; i++) {//遍历原始数组
boolean flag = true;
if ("".equals(str)) {//直接把原始数组的第一位添加到字符串
flag = false;
str += arr[i];
continue;
}
for (int j = 0; j < str.split(",").length; j++) {//遍历字符串转成的数组
if (arr[i] == Integer.parseInt(str.split(",")[j] + "")) {//如果原始数组的数字与符串转成的数组每个数字有相同的,给一个状态,并结束本次循环
flag = false;
continue;
}
}
if (flag) {
str += "," + arr[i];
}
}
System.out.println(str);
}
}
打印3,7,9,8,2,0
public class Ddq {
public static void main(String[] args) {
int arr[] = {3, 3, 3, 7, 7, 7, 9, 8, 2, 2, 7, 7, 3, 3, 0, 8};
String str = "";
for (int i = 0; i < arr.length; i++) {//遍历原始数组
boolean flag = true;
if ("".equals(str)) {//直接把原始数组的第一位添加到字符串
flag = false;
str += arr[i];
continue;
}
for (int j = 0; j < str.split(",").length; j++) {//遍历字符串转成的数组
if (arr[i] == Integer.parseInt(str.split(",")[j] + "")) {//如果原始数组的数字与符串转成的数组每个数字有相同的,给一个状态,并结束本次循环
flag = false;
continue;
}
}
if (flag) {
str += "," + arr[i];
}
}
System.out.println(str);
}
}
打印3,7,9,8,2,0
相关文章推荐
- android学习5#--自定义View之坐标系统
- 使用Fiddler做抓包分析
- android .9制作
- js继承方式
- 微信公众平台开发(71)OAuth2.0网页授权
- [leetcode] 270. Closest Binary Search Tree Value 解题报告
- UIViewContentMode
- Tomcat 6.0 部署与发布
- UIAutomator
- [leetcode] 【字符串】 12. Integer to Roman
- Java实现线程的两种方式
- Codeforces Round 355 div2 (E abandoned)677ABCD
- wps插入批注快捷键是哪个?
- 对象的序列化与反序列化
- iOS开发之缓存框架、内存缓存、磁盘缓存、NSCache、TMMemoryCache、PINMemoryCache、YYMemoryCache、TMDiskCache、PINDiskCache
- 6.15
- Siri Integration in iOS 10 with Swift – SiriKit Tutorial (Part 1)
- hibernate.cfg.xml
- C语言程序报告
- ios学习路线—Objective-C(检查内存泄露方式(Enable NSZombie Objects))