简体   繁体   English

Java-结构交叉点的ArrayList

[英]Java - ArrayList of structure Intersections

I need to call GetNewItem function million times; 我需要调用GetNewItem函数一百万次;

Items XY = GetNewItem(X, Y); 
Items XYZ = GetNewItem(XY, Z); 
Items XZ = GetNewItem(X, Z); 
Items YZ = GetNewItem(Y, Z); 

This function aims to 1- find intersection between ArrayList of structure namely 该功能旨在1-即找到结构的ArrayList之间的交集

ArrayList<Records> RecordLists

2- and it also calculates the probability for the new ArrayList , this is my code: 2-并且还计算新ArrayList的概率,这是我的代码:

 class Records {

        public int RecordId;
        public double Prob;
    }

    class Items {
        public ArrayList<Integer> itemId;
        public ArrayList<Records> RecordLists;
        public double ItemProb = 0.0;

    };

 private ArrayList<Records> Intersection(ArrayList<Records> list1, ArrayList<Records> list2) {
        ArrayList<Records> Result = new ArrayList<>();

            int i = 0, j = 0;
            while (i < list1.size() && j < list2.size()) {
                if (list1.get(i).RecordId== (list2.get(j).RecordId)) {
                    Records RecordDetails= new Records();
                    RecordDetails.RecordId= list1.get(i).RecordId;
                    RecordDetails.Prob+= 1;
                    Result.add(RecordDetails);

                    i++;
                    j++;
                } else if (list1.get(i).RecordId < list2.get(j).RecordId) {
                    i++;
                } else if (list1.get(i).RecordId > list2.get(j).RecordId) {
                    j++;
                }

            }

        return Result;
    }

    public Items GetNewItem(Items item1, Items item2) {
        Items NewItem = new Items ();
        ArrayList<Integer> newItemId = new ArrayList<>();
        newItemId.addAll(item1.itemId);
        newItemId.addAll(item2.itemId);

        NewItem.itemId = newItemId;

        NewItem.RecordLists= Intersection(item1.RecordLists,item2.RecordLists);

        NewItem.ItemProb = getProb(NewItem.RecordLists);          
        return NewItem ;
    }

    private double getProb(ArrayList<Records> RProb) {
        double IProb = 0.0;
        for (int i = 0; i < RProb.size(); i++) {
            IProb += RProb.get(i).Prob;
        }
        return IProb ;
    }

For this code I got 'out of memory error' 对于此代码,我遇到了“内存不足错误”

I don't know how to save the memory and time, I tried this solution: java.lang.OutOfMemoryError: Java heap space with NetBeans 我不知道如何节省内存和时间,我尝试了以下解决方案: java.lang.OutOfMemoryError:带NetBeans的Java堆空间

but my computer did freeze. 但是我的电脑死机了。 I don't know what else I have to do. 我不知道我还要做什么。

  1. Please use java conventions, eg variables in camel case, non public variables in classes (use getters/constructor/setters) 请使用Java约定,例如,驼峰式变量,类中的非公共变量(使用getters / constructor / setters)

  2. I'm not sure why are you getting the intersection that way, with that i and j variables. 我不知道为什么用i和j变量那样得到交集。 Please try: 请试试:

     public <T> List<T> intersection(List<T> list1, List<T> list2) { List<T> list = new ArrayList<T>(); for (T t : list1) { if(list2.contains(t)) { list.add(t); } } return list; } 

If you want to calculate something else maybe do it in a separate method? 如果您想计算其他内容,也许可以使用单独的方法?

  1. Use floats instead of doubles. 使用浮点数而不是双精度数。

Could you please paste whole code? 您能粘贴整个代码吗? I would like to reproduce this. 我想重现这一点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM