简体   繁体   English

从方法返回时 java 列表为空

[英]java list empty when returning from method

I have a method that loops through a sorted Array and those sorted List elements to another list called sortedFirstName.我有一个方法可以遍历排序的数组和排序的列表元素到另一个名为 sortedFirstName 的列表。 Here is that methods code.这是方法代码。

 public void sortFirstName(String input){
       Collections.sort(fullName, new FirstNameComparator(input)); 

       for(int i = 0;i<fullName.size();i++){
           System.out.println("\n" + fullName.get(i));
           sortedFirstName.add(fullName.get(i)); //adds to list fine

       }
        System.out.println("Sorted First Name" + sortedFirstName);
        //prints desired output, [Jake Paul, Billy Robinson, Charles Princeton, John Paul, Karen Jacobs]

FirstName is a List and is instantiated like so - List<String> fullName = new ArrayList<String>(); FirstName 是一个 List 并且像这样被实例化 - List<String> fullName = new ArrayList<String>(); and is populated from a method listAllContacts like so:并从方法 listAllContacts 填充,如下所示:

public void listAllContacts(){

        for (Map.Entry<String,Person> entry : contacts.entrySet()) {
         fullName.add(entry.getValue().fullName());
}
        for (String contents : fullName) {                
           System.out.println("\n" + contents);         
      }
    }

it is populated by Strings that are a field of an object in a hashmap它由字符串填充,这些字符串是 hashmap 中 object 的字段

from this method, I print the contents of list sortedFirstName list and get the correct output which is commented under the print statement.通过这种方法,我打印 list sortedFirstName list 的内容并得到正确的 output ,它在 print 语句下被注释。

I also have this get method that returns that list我也有这个返回该列表的 get 方法

public List<String> getSortedFirstNames(){
        return this.sortedFirstName;
    }

In my Junit class I am calling these methods like so在我的 Junit class 我这样称呼这些方法

@Test
    public void sortFirst(){
        ad1.sortFirstName("Jake");
        List sorted = ad1.getSortedFirstNames();
        List sortedList = new ArrayList();
        sortedList.addAll(Arrays.asList("Jake Paul", "Billy Robinson", "Charles Princeton", "John Paul", "Karen Jacobs")); //expected output from boostrapped users
        assertEquals(sortedList,sorted);
    }

From my test case logic it should be that List sorted contains the elements [Jake Paul, Billy Robinson, Charles Princeton, John Paul, Karen Jacobs] but when I compare my hardcoded list against sorted , it shows as empty.从我的测试用例逻辑来看, List sorted应该包含元素[Jake Paul, Billy Robinson, Charles Princeton, John Paul, Karen Jacobs]但是当我将硬编码列表与sorted进行比较时,它显示为空。 I cannot see an issue as sortedFirstName clearly has the elements added to it, Any help would be appreciated我看不到问题,因为sortedFirstName清楚地添加了元素,任何帮助将不胜感激

I have spotted my mistake.我发现了我的错误。 The listAllContacts method is needed to actually populate the fullName variable.实际填充fullName变量需要listAllContacts方法。 Without the listAllContacts method being called it does not populate the fullName and in turn cannot populate the sortedFirstName list.如果没有调用listAllContacts方法,它不会填充fullName ,进而无法填充sortedFirstName列表。

Here is my new code:这是我的新代码:

@Test
    public void sortFirst(){
        ad1.listAllContacts();
        ad1.sortFirstName("Jake");
        List sorted = ad1.getSortedFirstNames();
        List sortedList = new ArrayList();
        sortedList.addAll(Arrays.asList("Jake Paul", "Billy Robinson", "Charles Princeton", "John Paul", "Karen Jacobs")); //expected output from boostrapped users
        assertEquals(sortedList,sorted);
    }

note the ad1.listAllContacts();注意ad1.listAllContacts(); which fixed my issue.这解决了我的问题。 Thanks to all who commented ti help me with this, much appreciated.感谢所有评论的人帮助我解决这个问题,非常感谢。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM