在同一类型的一个数组中比较并合并myClass类型的2个数组

时间:2018-08-15 23:34:03

标签: java arrays json

我正在尝试合并2个已获取json URL数据的数组,这2个url具有1个共享字段,称为“ bay_id”,该字段可匹配3080个项目。 URL 1具有3080数据,URL 2具有7100数据。我确实比较了URL 1中的“ bay_id”在URL 2中是否具有相同的“ bay_id”,然后进行了合并。我的for循环遇到的问题是,它为所有索引设置了相同的“ bay_id”。除了名为Post的myClass外,所有所需字段都具有setter和getter方法。 我的代码是:

for (int i = 0; i < url_1.length; i++) {

                for (int j = 0; j < url_2.length; j++) {

                    if (url_2[j].getBayId_2().equals(url_1[i].getBayId_1()) ) {

                        for (int k = 0; k < mergedURLs.length; k++) {

                            mergedURLs[k].setBayId_1(url_1[i].getBayId_1());
                            mergedURLs[k].setStatus(url_1[i].getStatus());
                            mergedURLs[k].setLat(url_1[i].getLat());
                            mergedURLs[k].setLon(url_1[i].getLon());
                            mergedURLs[k].setBayId_2(url_2[j].getBayId_2());
                           //and so on for all the fields 
                    }//-------End if statment 
                }//------End for loop url_2
            }//-------end for loop url_1
        }//-------end for loop 


        for (int z = 0; z < mergedURLs.length; z++) {
            System.out.println("New bay_id" + mergedURLs[z].getBayId_1());
        }

1 个答案:

答案 0 :(得分:0)

这是一种执行此操作的方法。我创建了一个名为MyUrl的测试类,它代表数组(urls1urls2)和合并数组中的对象。这个MyUrl类实现了java.lang.Comparable接口-用于对url1数组进行排序(更多详细信息,请参见下文)。

合并过程:我假设urls2数组比urls1具有更多数据。首先,我创建了一个空ArrayList<MyUrl>,它将包含所有URL-合并的URL。然后 sort urls1数组-这是通过urls1中的bayIdurls2搜索Arrays.binarySearch数组-urls2方法是使用。

现在,在for循环中迭代urls1,并在bayId中搜索每个urls2bayId)的匹配项。一旦找到匹配的ArrayList,数据将合并并存储在urls2中,否则仅存储ArrayList数据。到for循环结束时,ArrayList将具有合并的数据。

下面的示例程序可以编译并运行。查看输入数组中的数据以及合并后的MyUrl的打印输出。匹配并合并的import java.util.*; public class CompareAndMerge { // Some test data: two sets of url data. One with lesser urls1 and // the other usrls2. Note there are some matching bayIds in both arrays. private static MyUrl [] urls1 = {new MyUrl("abc", "yes"), new MyUrl("xyz", "no"), new MyUrl("url", "true"), new MyUrl("lmn", "false")}; private static MyUrl [] urls2 = {new MyUrl("abc", "yes"), new MyUrl("xyz", "no"), new MyUrl("cat", "else"), new MyUrl("rat", "if"), new MyUrl("url", "true"), new MyUrl("lmn", "false")}; public static void main(String [] args) { // The urls1 array is sorted by bayId to allow a binary search - // the searching of array by bayId. // Also, see the method getMatchingUrl() Arrays.sort(urls1, null); // null indicates the elements implement comparable // The list is to store the merged urls from bouth the arrays List<MyUrl> merged = new ArrayList<>(); // Iterate over the urls2 (the array with more elements) and store // the merged data in the list. The data is merged when there // is a match with bayId otherwise just the urls2 data is stored. for (MyUrl url2 : urls2) { MyUrl url1 = getMatchingUrl(url2); if (url1 == null) { // no bayId match found // store the url2 to merged urls merged.add(url2); } else { // found a matching bayId in urls1 // merge the url1 to url2 data // for example: url2.setBayId2(url1.getBayId2()); etc. url2.setMerged("MERGED"); merged.add(url2); } } // Result shows matching bayId's urls are merged. // The output has all the urls from both the arrays. System.out.println(merged); } private static MyUrl getMatchingUrl(MyUrl url2) { int urls1Index = Arrays.binarySearch(urls1, url2, null); // null indicates comparable elements if (urls1Index < 0) { return null; } return urls1[urls1Index]; } } class MyUrl implements Comparable<MyUrl> { private String bayId; private String status; private String merged; public MyUrl(String s1, String s2) { bayId = s1; status = s2; } public void setBayId(String s) { bayId = s; } public String getBayId() { return bayId; } public void setStatus(String s) { status = s; } public String getStatus() { return status; } public void setMerged(String s) { merged = s; } @Override public String toString() { return bayId + ":" + status + ":" + merged; } /* * Implemented method of java.lang.Comparable */ @Override public int compareTo(MyUrl url) { return this.bayId.compareTo(url.getBayId()); } } 具有“ MERGED”标志。

示例代码:

hid.node