如何使用另一个类中的方法从一个java类中的MD数组中删除重复项

时间:2021-02-28 04:46:00

Having to ask this question because I am a fool and overwrote old work.

不得不提出这个问题,因为我是个傻瓜,并且过分陈旧的工作。

Right now what I need to do is loop through a multidimensional array in one class, then loop through an arraylist(thats currently empty) and use an if statement to check whether or not there are duplicates inside that arraylist, if there aren't, then it will add the record to the arraylist, if it is, it will simply make isFound = false

现在我需要做的是在一个类中循环一个多维数组,然后遍历一个arraylist(当前为空)并使用if语句来检查该arraylist中是否有重复项,如果没有,然后它会将记录添加到arraylist,如果是,它将简单地使isFound = false

This is the method that will add the records to the arraylist. Right now it only works up to the second loop. this is the main class, called EAC

这是将记录添加到arraylist的方法。现在它只适用于第二个循环。这是主要的类,称为EAC

 public void PopulateRecords()
    {
        ArrayList<String> categories = new ArrayList<String>();
        for (int i = 0; i < Data.stats.length; i++)
        { //System.out.println(Data.stats[i][1]);
            for (String category : categories)
            {
                boolean isFound = false;
                if (Data.stats[i][1].equals(category))
                {
                    isFound = true;
                }
                if (!isFound)
                {
                    categories.add(Data.stats[i][0]);
                    System.out.println(categories);
                }
            }

        }
    }

This is the Category class, and the GetCategory here was used within the populaterecords() method somehow, but that's the one stage of this i'm not fully understanding, because there's a bit or two missing from here that's presumably preventing the method from working

这是Category类,这里的getCategory以某种方式在populaterecords()方法中使用,但这是我不完全理解的一个阶段,因为这里有一两个缺失可能阻止该方法工作

public class Category
{

    public String categoryname;
    public Category categories;

    public static void main(String[] args)
    {
        new Category();
    }

    public Category()
    {
    }

    public String GetCategory()
    {
        return categoryname;
    }

    public void SetCategory()
    {
    }
}

This is as specific as I can go, I'm by every definition a pure newbie at java, so any help here is much appreciated

这是我可以去的具体,我的每个定义都是java的纯新手,所以在这里任何帮助都非常感激

1 个解决方案

#1


1  

You're looping through an empty ArrayList, so the 2nd loop body will execute 0 times.

你循环遍历一个空的ArrayList,所以第二个循环体将执行0次。

ArrayList<String> categories = new ArrayList<String>();
for (int i = 0; i < Data.stats.length; i++)
{ //System.out.println(Data.stats[i][1]);
    for (String category : categories) // Here categories is empty, so no loop iterations occur

#1


1  

You're looping through an empty ArrayList, so the 2nd loop body will execute 0 times.

你循环遍历一个空的ArrayList,所以第二个循环体将执行0次。

ArrayList<String> categories = new ArrayList<String>();
for (int i = 0; i < Data.stats.length; i++)
{ //System.out.println(Data.stats[i][1]);
    for (String category : categories) // Here categories is empty, so no loop iterations occur