java: List of String Arrays and remove

前端 未结 4 683
我在风中等你
我在风中等你 2021-01-11 19:01

In a test like this:

    @Test
    public void test() {
        List l = new LinkedList();
        l.add(new String [] {\"tes         


        
4条回答
  •  星月不相逢
    2021-01-11 19:31

    The List method are often based on the Object method equals(Object o) which compare the object's reference by default. You may store the tab if you want remove it later, or create your own class and override equals(Object o) ;)

        @Test
        public void test() {
            List l = new LinkedList();
            l.add(new String [] {"test", "123"});
            String[] tab = new String [] {"test", "456"};
            l.add(tab);
            l.add(new String [] {"test", "789"});
    
            assertEquals(3, l.size());
    
            l.remove(tab);
    
            assertEquals(2, l.size());
        }
    

提交回复
热议问题