2014-01-11 54 views
1

好吧,這裏是我的要求,我有如下表:如何檢查字符串數組的連續性,如果它不在連續,然後刪除它?

 
ID-Category-No 
22-Software-1 
45-Software-2 
78-Hardware-3 
48-Software-4 
11-Hardware-5 
91-Service -6 
95-Service -7 
93-Software-8 

我通過無選擇的所有數據與命令,&把它變成一個String [],然後把這個字符串[] INT列表

因此,我將有:

String[] s1={"22","Software","1"}; 
String[] s2={"45","Software","2"}; 
..... 
List<String[]> myL=new ArrayList<String[]>(); 
myL.add(s1); 
myL.add(s2); 
.... 

現在,從myL,我只想把所有具有類別的連續性,如果該類別是不是在連續性中的String [],然後將其刪除。

需要注意的是:我們必須首先採用連續出現在同一類別中的所有類別。所以我們需要做一些事情,使最終myL將只包含這些的String []

 
ID-Category-No 
22-Software-1 
45-Software-2 
78-Hardware-3 
91-Service -6 
95-Service -7 
+0

當你說連續體時,你的意思是設置? –

+0

不,它只是意味着我想要繼續行,沒有任何關係設置 – Tum

+0

你的意思是連續的 –

回答

2

如果你不想任務更加複雜,你真的應該創建一個類,說Job,以存儲這些屬性,而不是使用String[]。然後保持List<Job>

Job類中,應該執行equals()hashCode()方法,根據category進行比較。因此,類將是這樣的:

class Job { 
    private int id; 
    private String category; 
    private int no; 

    public Job(int id, String category, int no) { 
     this.id = id; 
     this.category = category; 
     this.no = no; 
    } 

    @Override 
    public boolean equals(Object obj) { 
     if (!(obj instanceof Job)) return false; 
     return category.equals(((Job)obj).category); 
    } 

    @Override 
    public int hashCode() { 
     return category.hashCode(); 
    } 

    @Override 
    public String toString() { 
     return "[" + id + ", " + category + ", " + no + "]\n"; 
    } 
} 

現在,你可以有一個List<Job>

Job s1= new Job(22,"Software",1); 
Job s2= new Job(45,"Software",2); 
Job s3= new Job(78, "Hardware", 3); 
Job s4= new Job(48, "Software", 4); 
Job s5= new Job(11, "Hardware", 5); 
Job s6= new Job(91, "Service", 6); 
Job s7 =new Job(95, "Service", 7); 
Job s8 = new Job(93, "Software", 8); 

List<Job> list=new ArrayList<Job>(Arrays.asList(s1, s2, s3, s4, s5, s6, s7, s8)); 

創建一個臨時ArrayList,將存儲你的結果:

List<Job> contiguous = new ArrayList<Job>(); 

/* The boolean variable will be used to track whether the sequence till 
    now is contiguous or not */ 
/* Everytime we add a new unique job, a new contiguous sequence is started. 
    So, we will set this variable to `true` */ 
boolean isContiguousTillNow = false; 

這裏的執行實際工作的循環:

for (Job job: list) { 

    if (contiguous.isEmpty()) { 
     /* First job added. Set 'isContiguousTillNow' to 'true' */ 
     contiguous.add(job); 
     isContiguousTillNow = true; 

    } else if (isContiguousTillNow && contiguous.get(contiguous.size() - 1).equals(job)) { 
     /* The sequence has been contiguous till now, and the last job 
      added is equal to current job. Add the job. */ 
     contiguous.add(job); 

    } else if (!contiguous.contains(job)) { 
     /* Sequence is either broken here, or was already broken */ 
     /* But since the list doesn't already contains this job, add it, 
      and this starts a new contiguous sequence. So, set it to 'true' */ 
     contiguous.add(job); 
     isContiguousTillNow = true; 

    } else { 
     /* ContigousSequence stops here. So, set it to 'false' */ 
     isContiguousTillNow = false; 
    } 
} 

然後您的contiguous列表將包含所需的結果。

+0

我們的解決方案非常明確,非常感謝 – Tum

1
public void methodX() { 
    String[] s1 = {"22", "Software", "1"}; 
    String[] s2 = {"45", "Software", "2"}; 
    String[] s3 = {"78", "Hardware", "3"}; 
    String[] s4 = {"48", "Software", "4"}; 
    String[] s5 = {"11", "Hardware", "5"}; 
    String[] s6 = {"91", "Service", "6"}; 
    String[] s7 = {"95", "Service", "7"}; 
    String[] s8 = {"93", "Software", "8"}; 
    ArrayList<String[]> myL = new ArrayList<>(); 
    myL.add(s1); 
    myL.add(s2); 
    myL.add(s3); 
    myL.add(s4); 
    myL.add(s5); 
    myL.add(s6); 
    myL.add(s7); 
    myL.add(s8); 
    HashSet<String> set = new HashSet(); 
    boolean continued = false; 
    String category = ""; 
    ArrayList<String[]> toRemove = new ArrayList<>(); 
    for (String[] s : myL) { 
     if (!continued) { 
      if (set.add(s[1])) { 
       continued = true; 
       category = s[1]; 
      } else { 
       continued = false; 
       toRemove.add(s); 
      } 
     } else { 
      if (s[1].equals(category)) { 

      } else { 
       if (set.add(s[1])) { 
        continued = true; 
        category = s[1]; 
       } else { 
        continued = false; 
        toRemove.add(s); 
       } 
      } 
     } 
    } 
    myL.removeAll(toRemove); 
    for (String[] list : myL) { 
     System.out.println(list[0] + "-" + list[1] + "-" + list[2]); 
    } 
} 
0

這是刪除的方法。我編寫了一個保留方法。

public void myMethod() 
{ 
String[] s1 = {"22", "Software", "1"}; 
String[] s2 = {"45", "Software", "2"}; 
String[] s3 = {"78", "Hardware", "3"}; 
String[] s4 = {"48", "Software", "4"}; 
String[] s5 = {"11", "Hardware", "5"}; 
String[] s6 = {"91", "Service", "6"}; 
String[] s7 = {"95", "Service", "7"}; 
String[] s8 = {"93", "Software", "8"}; 
ArrayList<String[]> myL = new ArrayList<>(); 
myL.add(s1); 
myL.add(s2); 
myL.add(s3); 
myL.add(s4); 
myL.add(s5); 
myL.add(s6); 
myL.add(s7); 
myL.add(s8); 
List<String> list = new ArrayList<String>(); 
List<String[]> retainList = new ArrayList<String[]>(); 
String previous = ""; 
for (String[] s : myL) 
{ 
    if(list.size() == 0) 
    { 
     previous = s[1]; 
     add(list, retainList, s); 
     continue; 
    } 
    if(previous.equals(s[1])) 
     add(list, retainList, s); 
    else 
     if(!list.contains(s[1])) 
      add(list, retainList, s); 
    previous = s[1]; 
} 
myL.retainAll(retainList); 
for(String[] r : myL) 
    System.out.println(r[0] + "-" + r[1] + "-" + r[2]) ; 
} 

public void add(List<String> t, List<String[]> r, String[] s) 
{ 
t.add(s[1]); 
r.add(s); 
} 
相關問題