2016-08-16 56 views
-2

我有一个模型:在ArrayList中获取索引第一个和最后一个项目?

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

你的问题不清楚,请描述你需要的东西 – Smit

+2

请提供你到目前为止尝试过的代码。 SO不是给网站的代码。它相当于一个解决问题的平台 –

+1

这看起来像一个纯粹的Java问题,因此这里不需要Android标签。 – EJK

回答

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)); 
} 
} 
+0

。如果不希望第一次排序,那么我需要找到没有排序。谢谢你,如果你帮我。然后附加所有吉姆姓名结束的列表。 – Rose

0

为了得到最后一个索引,你可以使用HashMap和key作为名字和值作为索引。输入任何名称时,如果它存在更新其索引,则输入该名称的值作为索引。

为了存储第一次出现,请使用另一张地图,并在其中存储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++; 
} 
1

您可以使用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); 

      } 
    } 

} 

输出:

  1. 插孔值从索引0和最多索引4开始
  2. 萨拉值从索引6和最多索引8
  3. 开始
  4. ricky值从索引10开始到索引12

注意: - 此代码将在这个问题给出的输入工作,你必须调整逻辑点点,如果你想这对其他ArrayList的工作。 我已经在代码中添加了注释,即使那样,如果您不明白,调试代码的逻辑很简单。

您可以检查输出here

+0

@ Ravikumar。我需要你的帮助。你能帮我吗? – Rose

+0

@玫瑰好吧,我可以帮忙吗? – Ravikumar

+0

请在聊天室中进行聊天。 – Rose

相关问题