简体   繁体   中英

How can I check whether a value from one array exists in another array?

I have 3 arrays. One counts population, one sorts the population using arrays.sort and the other one stores strings. I want to essentially sort the strings from least to highest. Knowing that the value of where one population is stored is the same as where one string is stored i tried to match the sorted values to the population like this, it it seems to output the strings in the order that I wrote them. Where did I go wrong?

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]);
                }
            }
        }
    }
}

You can iterate through both arrays and do a comparison, you can use Arrays.binarySearch() , you can convert the array to a List with Arrays.asList() and use contains() on the list, etc. Also careful, you are doing something strange here: if (population[i] < max) . That should compare with min , I think.

Instead of storing related information in separate variables, make a class and store information in the same object. Then write a comparator that can sort for you on the basis of the population count. This will also optimize your code.

Eg

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

you can sort your array using comparator like:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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