简体   繁体   中英

How to remove an element from an array based on a condition? Java

I have 2 sources of information from which I obtain different products to show. However, there are repeated (same identifier) and I would like to eliminate those. I have a flag which tells me which source I am getting and I would like to eliminate those that have repeated their identifier and the flag in 1. This is my array of objects that I get when I finish extracting the sources of information:

[
  {
    "identifier":"10410",
    "sku":"69343",
    "product":"Name 2",
    "flag":2,
    "date":null,
    "price":304.74
  },
  {
    "identifier":"10555",
    "sku":"69343",
    "product":"Period 2",
    "flag":2,
    "date":null,
    "price":304.74
  },
  {
    "identifier":"10410",
    "sku":"69388",
    "product":"Other name 2",
    "flag":1,
    "date":null,
    "price":304.74
  },
  {
    "identifier":"10444",
    "sku":"69341",
    "product":"Other name 3",
    "flag":1,
    "date":null,
    "price":304.74
  }
]

I try to make this:

[
  {
    "identifier":"10410",
    "sku":"69343",
    "product":"Name 2",
    "flag":2,
    "date":null,
    "price":304.74
  },
  {
    "identifier":"10555",
    "sku":"69343",
    "product":"Period 2",
    "flag":2,
    "date":null,
    "price":304.74
  },
  {
    "identifier":"10444",
    "sku":"69341",
    "product":"Other name 3",
    "flag":1,
    "date":null,
    "price":304.74
  }
]

Can anybody help me? Or give me advice on how to do it in Java?

Here is a simple example.

The model would be your constructor with setters and getters.

public class ModelExample {

        String title;

        public ModelExample(String title) {
            this.title = title;
        }

        public String getTitle() {
            return title;
        }

        public void setTitle(String title) {
            this.title = title;
        }
}

Let's create a List of that model, and add titles to it. You will do a for loop of your json array and add the values in. It'll be similar to this though.

List<ModelExample> model = new ArrayList<ModelExample>();
model.add(new ModelExample("instructor"));
model.add(new ModelExample("Dev"));
model.add(new ModelExample("Dev 2"));
model.add(new ModelExample("Dev 3"));
model.add(new ModelExample("Dev 4"));
model.add(new ModelExample("Dev 5"));
model.add(new ModelExample("Dev 6"));

If I Want to print everything out....

      for (int k = 0; k < model.size(); k++) {
          System.out.println("Title: " + model.get(k).getTitle());
      }

This will print the following:

Title: instructor
Title: Dev
Title: Dev 2
Title: Dev 3
Title: Dev 4
Title: Dev 5
Title: Dev 6

I then want to loop through the list, and I have a condition that if the index title is equal to instructor, i remove it.

for (int i = 0; i < model.size(); i++) {
    if (model.get(i).getTitle().equals("instructor")){
        model.remove(i);
    }
}

I then want to create a new JSONArray and add the remaining items in my list as a json object.

JSONArray array = new JSONArray();
    try {
        for (int i = 0; i < model.size(); i++) {
            JSONObject object = new JSONObject();
            object.put("title", model.get(i).getTitle());
            array.put(object);
        }


        System.out.println("JSONArray: " + array.toString());

    } catch (JSONException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }

The final print value will be this.

[{
    "title": "Dev"
}, {
    "title": "Dev 2"
}, {
    "title": "Dev 3"
}, {
    "title": "Dev 4"
}, {
    "title": "Dev 5"
}, {
    "title": "Dev 6"
}]

Here is my entire class I coded. Please keep in mind that you should always practice OOP. Create methods to remove the index instead of having it in the entire class. This is a very quick and messy way to do it.

import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

public class ModelExample {

        String title;

        public ModelExample(String title) {
            this.title = title;
        }

        public String getTitle() {
            return title;
        }

        public void setTitle(String title) {
            this.title = title;
        }


    public static void main(String[] args) {
        // TODO Auto-generated method stub
          List<ModelExample> model = new ArrayList<ModelExample>();
          model.add(new ModelExample("instructor"));
          model.add(new ModelExample("Dev"));
          model.add(new ModelExample("Dev 2"));
          model.add(new ModelExample("Dev 3"));
          model.add(new ModelExample("Dev 4"));
          model.add(new ModelExample("Dev 5"));
          model.add(new ModelExample("Dev 6"));


          for (int k = 0; k < model.size(); k++) {
              System.out.println("Title: " + model.get(k).getTitle());
          }

          System.out.println("\n\nSize of list before removing: " + model.size());

          for (int i = 0; i < model.size(); i++) {
              if (model.get(i).getTitle().equals("instructor")){
                  model.remove(i);
                  System.out.println("REMOVED INSTRUCTOR");
              }
          }

          System.out.println("Size of list after removing: " + model.size() + "\n\n");

          JSONArray array = new JSONArray();
          try {
              for (int i = 0; i < model.size(); i++) {
                  JSONObject object = new JSONObject();
                  object.put("title", model.get(i).getTitle());
                  array.put(object);
              }

              for (int j = 0; j < array.length(); j++) {
                  System.out.println("Title: " + array.get(j));
              }

              System.out.println(array.toString());
          } catch (JSONException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
          }
    }

}

And finally, this printed the following:

Title: instructor
Title: Dev
Title: Dev 2
Title: Dev 3
Title: Dev 4
Title: Dev 5
Title: Dev 6


Size of list before removing: 7
REMOVED INSTRUCTOR
Size of list after removing: 6


Title: {"title":"Dev"}
Title: {"title":"Dev 2"}
Title: {"title":"Dev 3"}
Title: {"title":"Dev 4"}
Title: {"title":"Dev 5"}
Title: {"title":"Dev 6"}
[{"title":"Dev"},{"title":"Dev 2"},{"title":"Dev 3"},{"title":"Dev 4"},{"title":"Dev 5"},{"title":"Dev 6"}]

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