如何检查字符串数组的连续性,如果它不在连续性中,则将其删除?



好的,这是我的要求,我有一个下表:

标识-类别-编号22-软件-145-软件-278-硬件-348-软件-411-硬件-591-服务 -695-服务 -793-软件-8

我按 No 顺序选择所有数据,并将其放入字符串 [] 中,然后将此 String[] 放入一个列表中

所以我会有:

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[]

标识-类别-编号22-软件-145-软件-278-硬件-391-服务 -695-服务 -7

如果你不想使任务复杂化,你应该真正创建一个类,比如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列表将包含所需的结果。

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

这是删除方法。我已经编写了一种保留方法。

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

最新更新