3
    List<String[]> sarray;
    ArrayList<ContentTable> currentData=new ArrayList<ContentTable>();

    //here sarray is initialized with data
    sarray = reader.readAll();


    for(String[] arr : sarray) 
        {
          System.out.println("array data "+ Arrays.toString(arr));
        }

        for(ContentTable ct : currentData)
        {
            System.out.println("list data "+ct.getId() +" "+ ct.getSubid() +" "+ct.getChpid()+" "+ct.getSec_name()+" "+ct.getContent());
        }   

输出数组和列表的 1 个结果:

数组数据-> [9, 10, 83, Concepts: 1-10, <p>We&#x2019;ll discuss many of the concepts in this chapter in depth later. But for now, we need a brief review of these concepts to equip you for solving exercises in the chapters that follow.</p>]

列出数据-> 9 10 83 Concepts: 1-10 <p>We&#x2019;ll discuss many of the concepts in this chapter in depth later. But for now, we need a brief review of these concepts to equip you for solving exercises in the chapters that follow.</p>

 //fields with getters and setters in ContentTable Class
        public class ContentTable {

            int id;
            int subid;
            int chpid;
            String sec_name;
            String content;
          }

现在我想要实现的是创建两个列表,

ArrayList<ContentTable> updatedData=new ArrayList<ContentTable>();
ArrayList<ContentTable> addedData=new ArrayList<ContentTable>();

这些将在比较之后填充数据,sarray并且currentdata以这样的方式,

如果ct.getSec_name()ct.getContent()在特定索引currentdata处不等于存在的数据,sarray则将其添加到updatedData

和,

如果ct.getId(), ct.getSubid(),ct.getChpid()在特定索引处不等于任何sarray数据,那么它将被添加到 addedData

以较低的复杂性来做这件事的优雅方法是什么,我想以最快的速度做到这一点,因为可能需要时间来比较每个元素Arraylist currentData以与ArrayList sarray.

4

2 回答 2

0

因此,解决我的问题的一个更好的方法是不将 aList<String[]>与进行比较ArrayList,而是转换List<String[]>为另一个Arraylist,然后将新Arraylist的与旧的进行比较。我发现这是非常简单和容易的方法。

                for(String[] arr : sarray) 
                {
                  ContentTable data=new ContentTable();

                  for(String s : arr)   
                  {
                         data.setId(Integer.parseInt(s));
                         data.setSubid(Integer.parseInt(s));
                         data.setChpid(Integer.parseInt(s));
                         data.setSec_name(s);
                         data.setContent(s);
                  }
                  newData.add(data);
                }

现在,newData 有了我想与 currentData 进行比较的新数据。

于 2013-07-15T06:57:29.947 回答
0

How about wrapping each element in array to Content. Now adding these content objects to Set. Iterate over the ArrayList and for each element in array list, check if its present in the set. If not update the addedData. If yes, get that object and compare both the content objects using equals.

Note that you would anyway override the hashCode and equals in Content type to make this(Set) work.

The one time activity that you do in the beginning will take time, which is getting the set ready. But once this set is ready, look ups with be really fast.

于 2013-07-06T10:24:33.453 回答