【问题标题】:How can I check whether a value from one array exists in another array?如何检查一个数组中的值是否存在于另一个数组中?
【发布时间】:2020-08-30 20:58:54
【问题描述】:

我有 3 个数组。一个计算人口,一个使用 arrays.sort 对人口进行排序,另一个存储字符串。我想基本上将字符串从最低到最高排序。知道一个群体的存储位置与一个字符串的存储位置相同,我尝试将排序后的值与这样的群体相匹配,它似乎按照我编写它们的顺序输出字符串。我哪里做错了?

import java.util.Arrays;
public class test {
    public static void main(String [] args){
        Scanner input = new Scanner(System.in);
        int Lvalue = 0;
        int max = 0;
        int min = 0;
        int Svalue = 0;

        System.out.println("How many countries will be entered");
        int num = input.nextInt();
        String[] country = new String[num];
        int[] population = new int[num];
        int[] popsort = new int[num];
        String[] capital = new String[num];

        for(int i = 0; i < num; i++)
        {
            System.out.println("Please enter the name of the country");
            country[i] = input.next();
            System.out.println("Please enter the country population");
            population[i] = input.nextInt();
            popsort[i] = population[i];
            System.out.println("Please enter the country capital");
            capital[i] = input.next();
        }
        for (int i = 0; i < num; i++)
        {
            if (population[i] > max)
            {
                max = population[i];
                Lvalue = i;
            }
        }
        for (int i = 0; i < num; i++)
        {
            if (population[i] < max)
            {
                min = population[i];
                Svalue = i;
            }
        }
        System.out.println("The country with largest pop is : " + country[Lvalue] + " ");
        System.out.println("The country with smallest pop is : " + country[Svalue] + " ");
        Arrays.sort(popsort);
        for(int i = 0; i < num; i++)
        {
            for (int j = 0; j < num; j++)
            {
                if (popsort[j] == population[i])
                {
                    System.out.println(country[i]);
                }
            }
        }
    }
}

【问题讨论】:

  • 请正确格式化您的代码。空白行使您的代码难以阅读。
  • 是的,我现在就这样做
  • 您的min 计算不正确。仅仅因为一个值小于最大值并不意味着它是最小值。例如,输入人口为 10、20 和 30 的 3 个国家。结果将告诉您第二个国家(人口 20)是最小值。如果您愿意,您实际上可以删除最小/最大循环并在输入期间跟踪这些值。
  • 另一个附注 - 您的文本条目只允许一个单词。使用,例如country[i] = input.next(); country[i] += input.nextLine(); 允许多个单词(大写相同)

标签: java arrays algorithm


【解决方案1】:

您可以遍历两个数组并进行比较,您可以使用Arrays.binarySearch(),您可以将数组转换为ListArrays.asList(),并在列表中使用contains(),等等。还要小心,你在这里做一些奇怪的事情:if (population[i] &lt; max)。我认为这应该与min 相比。

【讨论】:

  • 我尝试遍历两个数组以与此 for 循环进行比较,但它不起作用 `` for(int i = 0; i
  • 然后查看那个循环,因为那里肯定有一些不太对劲的地方。或者尝试任何其他方法。
【解决方案2】:

不要将相关信息存储在单独的变量中,而是创建一个类并将信息存储在同一个对象中。然后编写一个比较器,它可以根据人口计数为您排序。这也将优化您的代码。

例如

   class  Country {
     String name;
     int population;
     ...more fields.
    }

您可以使用如下比较器对数组进行排序:

   List<Country> countries; // let say you stored all countries in a list or array
   Arrays.sort(countries,(country1,country2)->{
    return  country1.getPopulation() - country2.getPopulation();
   }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-06-09
    • 1970-01-01
    • 1970-01-01
    • 2015-09-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-23
    相关资源
    最近更新 更多