从 ArrayList 中删除空值 [英] Remove null values from an ArrayList

查看:30
本文介绍了从 ArrayList 中删除空值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个按列排列的 ArrayList,当显示第一列ITEMS"时,每一列都是我数据库中的不同查询.它显示完美,但是当显示第二列材料"时它看起来像烦人的空值,它下面显示了正确的信息,会发生什么?我已经尝试了一切,但没有任何效果.

I have an ArrayList by columns, each column is a different query in my database, when showing column one "ITEMS" it shows perfect, but when showing column two "MATERIAL" it looks like the annoying null and below it shows the correct information, what happens? I have tried everything, but nothing works.

他尝试了 List.remove、List.RemoveIF,但没有任何效果

he tried with List.remove, List.RemoveIF, nothing works

private String[][] obtenerMatriz() {
    DetalleEntradaControlador detalleEntradaCon = new DetalleEntradaControlador();
    ArrayList<DetalleEntrada> lista = detalleEntradaCon.traerDesdeSAP();

    String matrizInfo[][] = new String[lista.size()][8];
    lista.removeAll(Collections.singleton(null));
    for (int i = 0; i < lista.size(); i++) {

        matrizInfo[i][0] = lista.get(i).getItem() + "";
        matrizInfo[i][1] = lista.get(i).getMaterial() + "";
        matrizInfo[i][2] = lista.get(i).getDescrip() + "";
        matrizInfo[i][3] = lista.get(i).getEdadPersona() + "";
        matrizInfo[i][4] = lista.get(i).getTelefonoPersona() + "";
    }
    return matrizInfo;
}

在我的方法 traerDesdeSAP 中,我有这个 Query 和 .add to list....

in my method, traerDesdeSAP, I have this Query and the .add to list....

public ArrayList<DetalleEntrada> traerDesdeSAP() {
    ArrayList<DetalleEntrada> lista = new ArrayList<DetalleEntrada>();
    DetalleEntrada detalle = null;

    try {
        Class.forName("com.sap.dbtech.jdbc.DriverSapDB");
        Conn = java.sql.DriverManager.getConnection(sapsource.getSAPSOURCE());
        s = Conn.createStatement();

        //TRAER ITEMS POR NUMERO DE DOC
        rs = s.executeQuery("...");
        while (rs.next()) {
            detalle = new DetalleEntrada();
            detalle.setItem(rs.getString(1));
            lista.add(detalle);
        }

推荐答案

你考虑过这样的事情吗?

Have you considered something like this?

List<String> list = new ArrayList<>();
list.add("A");
list.add(null);
list.add(null);
list.add("AB");
list.add("AC");
list.add(null);
list.add(null);
list.add(null);
list.add("AD");
list.add(null);
list.add("AE");
        
list.removeIf(o->o == null);
System.out.println(list);   

印刷品

[A, AB, AC, AD, AE]

请注意,条件可以是任何计算结果为布尔值的表达式.因此,您可以检查给定对象的任何和所有字段.

Note that the condition can be any expression that evaluates to a boolean. So you can check any and all fields of a given object.

这篇关于从 ArrayList 中删除空值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆