简体   繁体   中英

how can i remove duplicates from a MD array in one java class, using a method from another class

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

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

 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

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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