67,513
社区成员
发帖
与我相关
我的任务
分享
package com.cn.csdn;
public class E01_HelloWorld {
public static void main(String[] args) {
E01_HelloWorld test = new E01_HelloWorld();
test.ss();
}
int arr[] = { 3, 3, 4, 5, 5, 5, 6, 6, 7, 8, 8, 8, 8, 9, 10, 11, 11 };
int a = 0;//重复的是哪个数字
int n = 0;//重复几次
int adr = -1;//重复的第一位置
public boolean exist(int i) {
boolean isExist = false;
for (int k = 0; k <= i; k++) {
if (arr[i] == arr[k]) {
isExist = true;
}
}
return isExist;
}
public void ss() {
System.out.println("arr.length=" + arr.length);
for (int i = 0; i < arr.length; i++) {
if (exist(arr[i])) {
a = arr[i];
adr = i;
for (int j = i; j < arr.length; j++) {
if (a == arr[j]) {
n++;
}
}
System.out.println("========");
System.out.println(a + ":" + n + ":" + adr);
}
}
}
}
package test;
public class Number {
private int value;//值
private int beginIndex;// 重复的第一位置
private int count;// 重复次数
public int getValue() {
return value;
}
public void setValue(int value) {
this.value = value;
}
public int getBeginIndex() {
return beginIndex;
}
public void setBeginIndex(int beginIndex) {
this.beginIndex = beginIndex;
}
public int getCount() {
return count;
}
public void setCount(int count) {
this.count = count;
}
@Override
public boolean equals(Object obj) {
if(this==obj){
return true;
}
if(obj==null){
return false;
}
if(obj.getClass()!=this.getClass()){
return false;
}else{
final Number n = (Number)obj;
return n.getValue()==this.getValue();
}
}
@Override
public int hashCode() {
final int prime = 27;
int result = 7;
result = result * prime + value ^ 0x7777777;
return super.hashCode();
}
@Override
public String toString() {
return "【" + this.value + " : " + this.beginIndex + " : " + this.count + "】";
}
}
package test;
import java.util.ArrayList;
import java.util.List;
public class Test {
/**
* @param args
*/
public static void main(String[] args) {
List<Number> l = new ArrayList<Number>();
int[] arr = { 3, 3, 4, 5, 5, 5, 6, 6, 7, 8, 8, 8, 8, 9, 10, 11, 11 };
for(int i=0;i<arr.length;i++){
Number n = new Number();
n.setValue(arr[i]);
if(l.contains(n)){//若包含则直接取出来
n = l.get(l.indexOf(n));
}else{
n.setBeginIndex(i+1);//若不存在则 记录第一次出现位置并加入list
l.add(n);
}
n.setCount(n.getCount()+1);//重复次数加1
}
System.out.println(l);
}
}
public class HelloWord {
public static void main(String[] args) {
try {
HelloWord test = new HelloWord();
test.ss();
} catch (Exception e) {
e.printStackTrace();
}
}
int arr[] = { 3, 3, 4, 5, 5, 5, 6, 6, 7, 8, 8, 8, 8, 9, 10, 11, 11 };
int a = 0;// 重复的是哪个数字
int n = 0;// 重复几次
int adr = -1;// 重复的第一位置
public boolean exist(int i) {
boolean isExist = false;
try {
for (int k = 0; k < arr.length; k++) {
if (arr[i] == arr[k]) {
isExist = true;
break;
}
}
} catch (Exception e) {
e.printStackTrace();
}
return isExist;
}
public boolean has(int i) {
boolean has = false;
try {
for (int k = 0; k < i; k++) {
if (arr[i] == arr[k]) {
has = true;
break;
}
}
} catch (Exception e) {
e.printStackTrace();
}
return has;
}
public void ss() {
System.out.println("arr.length=" + arr.length);
try {
for (int i = 0; i <arr.length; i++) {
if (exist(arr[i])&&!has(i) ) {
a = arr[i];
adr = i;
for (int j = i; j<arr.length; j++) {
if (a == arr[j]) {
n++;
}
}
System.out.println("========");
System.out.println(a + ":" + n + ":" + adr);
n=0;
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}