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

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

问题描述

当按列显示第一列"ITEMS"时,我的列中有一个ArrayList,每列是数据库中的不同查询.它显示完美,但是当显示第二列"MATERIAL"时,它看起来像令人讨厌的null,并且在其下方显示了正确的信息,会发生什么?我已经尝试了一切,但没有任何效果.

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中,我有此查询和.add添加到列表....

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天全站免登陆