如何根据条件从数组中删除元素?爪哇

时间:2019-01-15 20:35:44

标签: java arrays spring-boot

我有2种信息来源,可以从中获得不同的产品来展示。但是,有重复的(相同的标识符),我想消除它们。 我有一个标志,告诉我我要获得哪个来源,我想消除那些重复了其标识符和1中的标志的人。 这是我完成提取信息源时得到的对象数组:

[
  {
    "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
  }
]

我试图做到这一点:

[
  {
    "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
  }
]

有人可以帮助我吗?或就如何用Java提出建议?

1 个答案:

答案 0 :(得分:0)

这是一个简单的例子。

模型将是具有setter和getter的构造函数。

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;
        }
}

让我们创建该模型的列表,并为其添加标题。您将对json数组进行for循环,然后将值添加到其中。尽管如此,该操作与此类似。

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());
      }

这将打印以下内容:

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

然后我想遍历列表,并且有一个条件,即如果索引标题等于讲师,则将其删除。

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

然后我想创建一个新的JSONArray并将列表中的其余项添加为json对象。

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();
    }

最终的打印值就是这个。

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

这是我编码的整个班级。请记住,您应该始终练习OOP。创建方法以删除索引,而不是将其包含在整个类中。这是一种非常快速且混乱的方法。

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();
          }
    }

}

最后,它打印了以下内容:

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"}]