list没有打印正确的值

sdnqo3pr  于 2021-06-26  发布在  Java
关注(0)|答案(2)|浏览(281)

我的代码有个小问题,我这里有一个数组

final String[]  participatingInstitutions = {
            "Australian Catholic University",
            "Australian College of Applied Psychology",
            "Australian National University",
            "Charles Darwin University",
            "Charles Sturt University",
            "CQUniversity",
            "Griffith University",
            "International College of Management, Sydney" ,
            "La Trobe University" ,
            "Macleay College" ,
            "Macquarie University" ,
            "MIT Sydney" ,
            "National Art School" ,
            "SAE Creative Media Institute" ,
            "SIBT",
            "Southern Cross University" ,
            "Torrens University Australia" ,
            "University of Canberra" ,
            "University of New England" ,
            "University of Newcastle" ,
            "University of Sydney" ,
            "University of Technology Sydney" ,
            "University of Wollongong" ,
            "UNSW Sydney" ,
            "UNSW Canberra at ADFA" ,
            "Western Sydney University"
    };

这是一个始终保持不变且无法编辑的数组,此数组将用于比较列表中的值
目前我的列表中,包含由大学名称组成的字符串值,后跟大学教授的课程

AUSTRALIAN CATHOLIC UNIVERSITY
Bachelor of Accounting and Finance North Sydney (MacKillop) 103710 CSP 3F/EqP 60.65 Y
Bachelor of Arts Strathfield (Mount St Mary) 103302 CSP 3F/EqP 58.50 Y
Bachelor of Arts Blacktown 103314 CSP 3F/EqP new N
Bachelor of Arts/Bachelor of Commerce Strathfield (Mount St Mary) 103303 CSP 4F/EqP 60.35 Y
Bachelor of Arts/Bachelor of Global Studies Strathfield (Mount St Mary) 103311 CSP 4F/EqP 60.00 Y
Bachelor of Arts/Bachelor of Laws North Sydney (MacKillop) 107001 CSP 5F/EqP 71.80 Y
Bachelor of Biomedical Science North Sydney (MacKillop) 107008 CSP 3F/EqP 59.00 Y
Bachelor of Biomedical Science/Bachelor of Business Administration North Sydney (MacKillop) 107011 CSP 4F/EqP 64.30 Y
Bachelor of Biomedical Science/Bachelor of Laws North Sydney (MacKillop) 107010 CSP 5F/EqP 70.20 Y
Bachelor of Business Administration North Sydney (MacKillop) 103706 CSP 3F/EqP 58.80 Y
Bachelor of Business Administration Strathfield (Mount St Mary) 103716 CSP 3F/EqP 58.55 Y
Bachelor of Business Administration/Bachelor of Global Studies North Sydney (MacKillop) 103707 CSP 4F/EqP 58.55 Y
Bachelor of Business Administration/Bachelor of Laws North Sydney (MacKillop) 107004 CSP 5F/EqP 71.95 Y
Bachelor of Commerce North Sydney (MacKillop) 103701 CSP 3F/EqP 58.95 Y
Bachelor of Commerce Strathfield (Mount St Mary) 103711 CSP 3F/EqP 59.10 Y
Bachelor of Commerce Blacktown 103722 CSP 3F/EqP new N
Bachelor of Commerce/Bachelor of Business Administration North Sydney (MacKillop) 103709 CSP 4F/EqP 58.55 Y
Bachelor of Commerce/Bachelor of Business Administration Strathfield (Mount St Mary) 103717 CSP 4F/EqP 58.90 Y
Bachelor of Commerce/Bachelor of Global Studies North Sydney (MacKillop) 103703 CSP 4F/EqP 60.70 Y
AUSTRALIAN NATIONAL UNIVERSITY
Bachelor of Accounting Canberra 133503 CSP 3F/6P <5 Y
Bachelor of Actuarial Studies Canberra 134403 CSP 3F/6P 99.95 Y
Bachelor of Advanced Computing (Honours) Canberra 135705 CSP 4F/8P 90.50 Y

我的代码试图
这是我的方法,我声明了一个for循环,从i=0,到列表的末尾。我声明了一个从j=0到j=participatinginstitutionarray size的嵌套for循环。然后,我将列表中的每个元素与参与机构进行比较,如果一个元素是在参与机构数组中找到的大学,我在i=i+1处启动for循环,现在我的问题是第三个for循环负责打印课程,但是这不起作用。
在我们的列表中查找有效的参与机构,一旦找到,我们将打印其课程。

for(int i = 0; i<dummyLineList.size(); ++i)
{
    System.out.println(dummyLineList.get(i));
        for(int k = 0; k<participatingInstitutions.length; ++k)
        {
           if(dummyLineList.get(i).equals(participatingInstitutions[k].toUpperCase()))
           {
               for(int j = i+1; j<dummyLineList.size(); ++j)
               {
                   if(dummyLineList.get(j).equals(participatingInstitutions[k].toUpperCase()))
                   {
                       System.out.println(dummyLineList.get(j));
                   }
               }
           }
        }

请注意,在我的代码dummylinelist中,是我上面提到的列表
我的问题
我正试图打印出每所大学的课程,所以对于澳大利亚天主教大学,我应该打印它的课程,直到它到达另一所大学(澳大利亚国立大学)

gt0wga4j

gt0wga4j1#

您可以考虑使用streamapi,它让您在一行中完成所有工作。
将数组设为list1(list)
让你的学校列表成为列表2(list)

list2
   .stream()
   .forEach(innerList-> { 
        innerList
              .stream()
              .filter(item -> list1.contains(item))
              .forEach(System.out::println);
    });

我写这篇文章是为了举例,也许你需要根据你的需求修改它。

myss37ts

myss37ts2#

基本上你的目标是建立一个 Map<String,List<String>> 其中key是学校名称和价值观列表,与大学支持的课程相对应。

List<String> participatingInstitutionsList = Arrays.asList(participatingInstitutions);
Map<String,List<String>> resultMap = new HashMap<>();
String universityName = null;
for(String s:dummyListLines){
   if(participatingInstitutionsList.contains(s)){
      universityName = s;
      resultMap.put(universityName, new ArrayList<>());
   } else {
      resultMap.get(universityName).add(s);
   }
}
return resultMap;

我建议你可以试试 Set<String> 对于参与机构也是如此。了解 Map 以及如何从中获得价值。希望这能帮助你展开:)

相关问题