我有一个模特:
public class Unprocessed_DistanceTime_D {
private String lat;
private String lng;
private String date;
private String time;
private String counts;
private String userCode;
public String getLat() {
return lat;
}
public void setLat(String lat) {
this.lat = lat;
}
public String getLng() {
return lng;
}
public void setLng(String lng) {
this.lng = lng;
}
public String getDate() {
return date;
}
public void setDate(String date) {
this.date = date;
}
public String getTime() {
return time;
}
public void setTime(String time) {
this.time = time;
}
public String getCounts() {
return counts;
}
public void setCounts(String counts) {
this.counts = counts;
}
public String getUserCode() {
return userCode;
}
public void setUserCode(String userCode) {
this.userCode = userCode;
}
}
我用波纹管数据填充我的模型:
DATE TIME LAT LNG COUNT
1395/04/26 12:13:44 34.6117392 50.8548033 7
1395/04/26 12:13:49 34.6117392 50.8548033 7
1395/04/26 12:13:58 34.6117392 50.8548033 7
1395/04/26 12:14:00 34.6117392 50.8548033 7
1395/04/26 12:14:22 34.6117392 50.8548033 7
1395/04/26 12:14:44 34.6117392 50.8548033 7
1395/04/26 12:14:49 34.6117392 50.8548033 7
1395/04/26 12:14:58 34.6113533 50.8551068 27
1395/04/26 12:14:58 34.6113533 50.8551068 27
1395/04/26 12:15:00 34.6113533 50.8551068 27
1395/04/26 12:15:22 34.6113533 50.8551068 27
1395/04/26 12:15:35 34.6322184 50.8596414 9
1395/04/26 12:15:44 34.6113533 50.8551068 27
1395/04/26 12:15:49 34.6113533 50.8551068 27
1395/04/26 12:15:58 34.6113533 50.8551068 27
1395/04/26 12:15:58 34.6113533 50.8551068 27
1395/04/26 12:16:00 34.6113533 50.8551068 27
1395/04/26 12:16:09 34.6322184 50.8596414 9
1395/04/26 12:16:22 34.6113533 50.8551068 27
1395/04/26 12:16:44 34.6113533 50.8551068 27
1395/04/26 12:16:49 34.6113533 50.8551068 27
1395/04/26 12:16:58 34.6113533 50.8551068 27
1395/04/26 12:16:58 34.6113533 50.8551068 27
1395/04/26 12:17:00 34.6113533 50.8551068 27
1395/04/26 12:17:22 34.6113533 50.8551068 27
1395/04/26 12:17:44 34.6113533 50.8551068 27
1395/04/26 12:17:49 34.6113533 50.8551068 27
1395/04/26 12:17:58 34.6113533 50.8551068 27
1395/04/26 12:17:58 34.6113533 50.8551068 27
1395/04/26 12:18:00 34.6113533 50.8551068 27
1395/04/26 12:18:22 34.6113533 50.8551068 27
1395/04/26 12:18:38 34.6322184 50.8596414 9
1395/04/26 12:18:44 34.6113533 50.8551068 27
1395/04/26 12:18:49 34.6113533 50.8551068 27
1395/04/26 12:18:51 34.6322184 50.8596414 9
1395/04/26 12:18:58 34.6113533 50.8551068 27
1395/04/26 12:18:58 34.6113533 50.8551068 27
1395/04/26 12:19:00 34.6113533 50.8551068 27
1395/04/26 12:19:01 34.6113415 50.8562128 22
1395/04/26 12:19:22 34.6113415 50.8562128 22
1395/04/26 12:19:44 34.6113415 50.8562128 22
1395/04/26 12:19:49 34.6113415 50.8562128 22
1395/04/26 12:19:58 34.6113415 50.8562128 22
1395/04/26 12:19:58 34.6113415 50.8562128 22
1395/04/26 12:20:00 34.6113415 50.8562128 22
1395/04/26 12:20:01 34.6113415 50.8562128 22
1395/04/26 12:21:41 34.6322184 50.8596414 9
1395/04/26 12:22:32 34.6113415 50.8562128 22
1395/04/26 12:22:36 34.6322184 50.8596414 9
1395/04/26 12:25:00 34.6113415 50.8562128 22
1395/04/26 12:25:04 34.6113415 50.8562128 22
1395/04/26 12:25:13 34.6113415 50.8562128 22
1395/04/26 12:25:13 34.6113415 50.8562128 22
1395/04/26 12:25:16 34.6113415 50.8562128 22
1395/04/26 12:25:16 34.6113415 50.8562128 22
1395/04/26 12:25:37 34.6113415 50.8562128 22
1395/04/26 12:25:37 34.6322184 50.8596414 9
1395/04/26 12:26:00 34.6113415 50.8562128 22
1395/04/26 12:26:04 34.6113415 50.8562128 22
1395/04/26 12:26:13 34.6113415 50.8562128 22
1395/04/26 12:26:20 34.6113415 50.8562128 22
1395/04/26 12:26:38 34.6113415 50.8562128 22
1395/04/26 12:26:38 34.6322184 50.8596414 9
1395/04/26 12:26:39 34.6113415 50.8562128 22
1395/04/26 12:29:46 34.6322184 50.8596414 9
我写了下面的代码,但是我不能为我获取相同的第一个和最后一个项目的索引,我使用你的代码,但我无法得到真正的答案。 我的数据是排序基准时间,我不喜欢改变它我应该不加改变地处理这些数据。
我的代码在这里:
public class ComputeDetailMoreActivity_E extends AppCompatActivity {
Context context;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_computedetailmore);
int count = 1;
int X = 2;
boolean flag = true;
int index1 = -1;
int index2 = -1;
Integer counter = 0;
for (int i = 0; i < listCDM.size() - 1; i++) {
if (listCDM.get(i).getDate().equalsIgnoreCase(listCDM.get(count).getDate()) &&
listCDM.get(i).getLat().equalsIgnoreCase(listCDM.get(count).getLat()) &&
listCDM.get(i).getLng().equalsIgnoreCase(listCDM.get(count).getLng()) &&
listCDM.get(i).getCounts().equalsIgnoreCase(listCDM.get(count).getCounts())) {
if (flag) {
index1 = i;
flag = false;
counter++;
}
} else {
if (X < listCDM.size()) {
if (!listCDM.get(count).getLat().equalsIgnoreCase(listCDM.get(X).getLat()) &&
!listCDM.get(count).getLng().equalsIgnoreCase(listCDM.get(X).getLng()) &&
!listCDM.get(count).getCounts().equalsIgnoreCase(listCDM.get(X).getCounts())||
!listCDM.get(count).getDate().equalsIgnoreCase(listCDM.get(X).getDate())) {
Log.i("ADFASFEWRWE", "C : " + listCDM.get(X).getCounts() + "**" +
listCDM.get(X).getTime());
index2 = i;
Log.i("ASDASRDWE",i + "");
int error = count;
counter = 2;
}
if (listCDM.get(count).getLat().equalsIgnoreCase(listCDM.get(X).getLat()) &&
listCDM.get(count).getLng().equalsIgnoreCase(listCDM.get(X).getLng()) &&
listCDM.get(count).getCounts().equalsIgnoreCase(listCDM.get(X).getCounts()) &&
listCDM.get(count).getDate().equalsIgnoreCase(listCDM.get(X).getDate())) {
index2 = i;
flag = true;
counter++;
}
Log.i("RRTTGGTT", counter + "");
if (counter == 2) {
Log.i("ASDWERWEDAVVGG", index1 + " : " + index2);
flag = true;
counter = 0;
index1 = -1;
index2 = -1;
}
}
}
count++;
X++;
}
}
}
我的出局是:
7 : 6
7 : 10
12 : 16
18 : 30
32 : 33
38 : 45
-1 : 46
-1 : 47
49 : 55
57 : 61
-1 : 62
答案 0 :(得分:0)
有一种方法可以解决你的问题...
您将需要使用集合并查找元素在列表中的显示方式......
public static void main(String[] args) {
List<String> list = new ArrayList<>();
list.add("jack");
list.add("jack");
list.add("jack");
list.add("jack");
list.add("jack");
list.add("jim");
list.add("sara");
list.add("sara");
list.add("sara");
list.add("jim");
list.add("ricky");
list.add("ricky");
list.add("ricky");
list.add("angel");
Collections.sort(list);
for (String string : new LinkedHashSet<>(list)) {
int firstIndex = list.indexOf(string);
int frequency = Collections.frequency(list, string);
System.out.println(string + " from: " + firstIndex + " to: " + (firstIndex + frequency - 1));
}
}
答案 1 :(得分:0)
为了获取最后一个索引,您可以使用HashMap作为名称和值作为索引。输入任何名称时,如果存在则更新其索引,否则输入该名称,并将值作为索引。
用于存储第一次出现时使用另一张地图,如果此地图中不存在该名称,则存储在该地图中。
HashMap<String,Integer> firstOccur=new HashMap<String,Integer>();
HashMap<String,Integer> lastOccur=new HashMap<String,Integer>();
int index=0;
for(String myList:list){
if(firstOccur.contains(myList)){
lastOccur.delete(myList);
lastOccur.add(myList,index);
} else{
firstOccur.add(myList,index);
lastOccur.add(myList,index);
}
index++;
}
答案 2 :(得分:0)
您可以使用Set
,因为它不允许重复。
代码:
List<String> list = new ArrayList<String>();
list.add("jack");
list.add("jack");
list.add("jack");
list.add("jack");
list.add("jack");
list.add("jim");
list.add("sara");
list.add("sara");
list.add("sara");
list.add("jim");
list.add("ricky");
list.add("ricky");
list.add("ricky");
Set<String> set = new HashSet<String>();
int j = 0;
int k = 0;
int twoContinuousDifferentValue = 0;
int size = list.size();
for (int i = 0; i < size; i++) {
String value = list.get(i);
// set.add returns true if the element does not exists.
if (set.add(value)) {
twoContinuousDifferentValue++;
if (i > 1 && twoContinuousDifferentValue == 1) {
String currentListvalue = list.get(j);
System.out.println(currentListvalue + " value starts from index " + j + " And up to index " + (i - k));
}
j = i; // storing last new index of second unique value.
} else {
twoContinuousDifferentValue = 0;
if (i + 1 < size) {
if (value.equalsIgnoreCase(list.get(i + 1))) {
k = 0;
} else {
k += 1; // to count continuous unique values (example sara and jim, as jim was already addded to set, the value of i will increase by one and when ricky gets added the value of i will be 10, so we have to subtract it by 2 to get sara's index.
}
}
if (i == size - 1) {
System.out.println(value + " value starts from index " + j + " And up to index " + i);
}
}
}
输出:
- jack值从索引0开始直到索引4
- sara值从索引6开始直到索引8
- ricky值从索引10开始直到索引12
醇>
注意: - 此代码适用于问题中给出的输入,如果您希望将其用于其他arraylist,则必须稍微调整一下逻辑。 我在代码中添加了注释,即使这样,如果你不理解调试代码,因为逻辑很简单。
您可以检查输出here。