【问题标题】:List is not printing the correct values when in nested conditional statement comparing values in a array在嵌套条件语句中比较数组中的值时,列表未打印正确的值
【发布时间】:2021-04-11 06:06:24
【问题描述】:

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

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 =参与机构数组大小的嵌套 for 循环。然后我将列表中的每个元素与参与机构进行比较,如果一个元素是在参与机构数组中找到的一所大学,我在 i = i+1 处开始一个 for 循环,现在这是我的问题是第三个 for 循环负责打印课程,但是这不起作用。

  1. 在我们的列表中寻找有效的参与机构,一旦找到,我们将打印其课程。

    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 中,是我上面所说的列表

我的问题

我正在尝试打印每所大学为澳大利亚天主教大学开设的课程,我应该打印它所拥有的课程,直到它到达另一所大学(澳大利亚国立大学)

【问题讨论】:

  • 也许我理解错了,但你的问题到底是什么?
  • 我正在尝试打印每所大学为澳大利亚天主教大学开设的课程,我应该打印它所拥有的课程,直到它到达另一所大学(澳大利亚国立大学)
  • 好的,哪个部分不工作?
  • 嗯,这是我的方法,我声明了一个 for 循环,从 i = 0 到列表的末尾。一旦我这样做了,我就声明了一个从 j = 0 到 j =参与机构数组大小的嵌套 for 循环。然后我将列表中的每个元素与参与机构进行比较,如果一个元素是在参与机构数组中找到的一所大学,我在 i = i+1 处开始一个 for 循环,现在这是我的问题是第三个 for 循环负责打印课程,但是这不起作用。
  • 听起来您使用的数据结构(数组)对您没有任何帮助。这种映射可能在 Map 中效果更好。像Map&lt;String, List&lt;String&gt;&gt; 这样的东西。键是学校,值列表包含课程。

标签: java arrays string conditional-statements


【解决方案1】:

您可以考虑使用 Stream API,它可以让您在一行中完成所有工作。

让你的数组为 list1(List)

让你的学校列表为 list2(List)

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

我写这个是为了一个例子,也许你可能需要根据你的要求来修改它。

【讨论】:

    【解决方案2】:

    基本上,您的目标是建立一个Map&lt;String,List&lt;String&gt;&gt;,其中键是机构名称,值列表对应于大学支持的课程。

    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&lt;String&gt;。阅读Map 以及如何从中获取价值。希望这可以帮助您展开:)

    【讨论】:

    • 我不敢相信,我没有看到这个。我会将其标记为正确答案,仅出于学习目的,我将如何使用数组和列表数据结构来实现它。
    猜你喜欢
    • 2013-05-01
    • 1970-01-01
    • 2012-04-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-25
    • 1970-01-01
    • 2019-08-08
    相关资源
    最近更新 更多