Java 是否获取ArrayList中第一个和最后一个相同项的索引?

Java 是否获取ArrayList中第一个和最后一个相同项的索引?,java,loops,for-loop,arraylist,foreach,Java,Loops,For Loop,Arraylist,Foreach,我有一个模型: 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 l

我有一个模型:

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++;

             }

      }
}

有一种方法可以解决您的问题

您将需要使用集合并找到元素在列表中出现的频率

例子:
publicstaticvoidmain(字符串[]args){
列表=新的ArrayList();
列表。添加(“杰克”);
列表。添加(“杰克”);
列表。添加(“杰克”);
列表。添加(“杰克”);
列表。添加(“杰克”);
列表。添加(“jim”);
列表。添加(“sara”);
列表。添加(“sara”);
列表。添加(“sara”);
列表。添加(“jim”);
列表。添加(“ricky”);
列表。添加(“ricky”);
列表。添加(“ricky”);
列表。添加(“天使”);
集合。排序(列表);
for(字符串:新LinkedHashSet(列表)){
int firstIndex=list.indexOf(字符串);
int frequency=Collections.frequency(列表,字符串);
System.out.println(字符串+”从“+firstIndex+”到“+(firstIndex+frequency-1));
}
}

要获取最后一个索引,可以使用HashMap,key作为名称,value作为索引。输入任何名称(如果存在)时,请更新其索引,否则请输入该名称并将值作为其索引

要存储第一个事件,请使用另一个映射,并在其中存储此映射中不存在的iff名称

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++;
}
HashMap firstoccurse=newhashmap();
HashMap lastOccurse=新HashMap();
int指数=0;
用于(字符串myList:list){
if(firstOccurse.contains(myList)){
删除(myList);
添加(myList,index);
}否则{
添加(myList,index);
添加(myList,index);
}
索引++;
}

您可以使用
设置
,因为它不允许重复

代码:

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);

            }
  }

}
List List=new ArrayList();
列表。添加(“杰克”);
列表。添加(“杰克”);
列表。添加(“杰克”);
列表。添加(“杰克”);
列表。添加(“杰克”);
列表。添加(“jim”);
列表。添加(“sara”);
列表。添加(“sara”);
列表。添加(“sara”);
列表。添加(“jim”);
列表。添加(“ricky”);
列表。添加(“ricky”);
列表。添加(“ricky”);
Set=newhashset();
int j=0;
int k=0;
int twoContinuousDifferentValue=0;
int size=list.size();
对于(int i=0;i1&&twoContinuousDifferentValue==1){
字符串currentListvalue=list.get(j);
System.out.println(currentListvalue+“值从索引“+j+”开始,一直到索引“+(i-k));
}
j=i;//存储第二个唯一值的最后一个新索引。
}否则{
twoContinuousDifferentValue=0;
如果(i+1<尺寸){
if(value.equalsIgnoreCase(list.get(i+1))){
k=0;
}否则{
k+=1;//要计算连续的唯一值(例如sara和jim,因为jim已经添加到set中,i的值将增加1,当ricky被添加时,i的值将为10,因此我们必须将其减去2才能得到sara的索引。
}
}
如果(i==大小-1){
System.out.println(值+“值从索引“+j+”开始,直到索引“+i”);
}
}
}
输出:

  • 插孔值从索引0开始,一直到索引4
  • sara值从索引6开始,一直到索引8
  • ricky值从索引10开始,一直到索引12
  • 注意:-此代码适用于问题中给出的输入,如果您希望此代码适用于其他arraylist,则必须稍微调整逻辑。 我在代码中添加了注释,即使您不理解调试代码,因为逻辑很简单


    您可以检查输出。

    您的问题不清楚,请描述您需要什么。请提供您迄今为止尝试过的代码。这不是一些提供代码的网站。这是一个解决问题的平台。这似乎是一个纯Java问题,因此这里不需要安卓标签。@EJK。我在安卓工作室工作。这不会影响任何方式的解决方案。无论您使用的IDE是什么,代码都是一样的。如果不想第一次排序,那么查找我需要不排序的查找。如果您帮助我,非常感谢。然后将所有jim的名字附加到列表的末尾。@Ravikumar。我需要您的帮助。您能帮我吗?@Rose好的,我能帮什么忙吗?请在聊天室中提交。邀请我,我有何感想能找到你吗?我不能和你说话。网站告诉我:你必须在堆栈溢出上有20个声誉才能在这里交谈。请参阅常见问题解答。
    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++;
    }
    
    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);
    
                }
      }
    
    }