簡體   English   中英

發出將元素從其他類添加到ArrayList的問題

[英]Issue adding elements to an ArrayList from a different class

可以做這樣的事情嗎? 假設我想添加我在CountrysTest中提供的值並將其添加到Countrys中的ArrayList中。 同樣,我如何引用aCountries為選項2打印,看到我在選項1中創建了它,所以我無法在其他任何地方訪問它。

這是我的界面

public interface CountriesInterface
{
    public String largestPop();
    public String largestArea();
    public String popDensity();
}

這是國家課

import java.util.*;
public class Countries implements CountriesInterface
{
    private final List<CountriesInterface> theCountries = new ArrayList<>();
    private String cName;
    private String finalPopName;
    private String finalAreaName;
    private String finalDensityName;
    private int cPop = 0;
    private int cArea = 0;
    private int popDensity = 0;
    private int popCounter = 0;
    private int areaCounter = 0;
    private int densityCounter = 0;
    public Countries(String cName, int cPop, int cArea, int popDensity)
    {
        this.cName = cName;
        this.cPop = cPop;
        this.cArea = cArea;
        this.popDensity = popDensity;
    }

    public String largestPop()
    {
        for(int i = 0; i < theCountries.size(); i++)
        {
            if(cPop > popCounter)
            {
                popCounter = cPop;
                finalPopName = cName;
            }    
        }
        return finalPopName;
    }

    public String largestArea()
    {
        for(int i = 0; i < theCountries.size(); i++)
        {
            if(cArea > areaCounter)
            {
                areaCounter = cArea;
                finalAreaName = cName;
            }    
        }
        return finalAreaName;
    } 

    public String popDensity()
    {
        for(int i = 0; i < theCountries.size(); i++)
        {
            if(popDensity > densityCounter)
            {
                densityCounter = popDensity;
                finalDensityName = cName;
            }    
        }
        return finalDensityName;
    } 

}

這是CountrysTest類

import java.util.*;
public class CountriesTest
{
    public static void main(String[] args)
        {             
            int population = 0;
            int area = 0;
            int density = 0;
            Scanner myScanner = new Scanner(System.in);            
            boolean done = false;
            do
            {
                System.out.println("1.  Enter a country \n2.  Print countries with the largest population, area, and population density \n3.  Exit");
                int choice = Integer.parseInt(myScanner.nextLine());
                if (choice == 1)
                {
                    System.out.print("Enter name of country: ");
                    String input1 = myScanner.nextLine();
                    System.out.print("Enter area of country in square kilometers: ");
                    String input2 = myScanner.nextLine();
                    population = Integer.parseInt(input2);
                    System.out.print("Enter population of country: ");
                    String input3 = myScanner.nextLine();
                    area = Integer.parseInt(input3);
                    density = population/area;
                    Countries aCountries = new Countries(input1, population, area, density);
                }
                else if(choice == 2)
                {
                    System.out.println("The country with the largest population: " );
                    System.out.println("The country with the largest area: "  );
                    System.out.println("The country with the largest population density is: "  );
                }
                else if(choice == 3)
                {
                     done = true;
                }              
                else
                    System.out.println("Invalid Choice");     
            }
            while (!done);
           System.exit(0);
        }
}

好的,您的代碼有些混亂。

  • 您同時將“ Countries”類用於單個“國家/地區”和國家/地區列表。 我不會推薦它,但是至少您應該使“靜態”成員和方法適用於國家/地區列表。 或者您可以聲明List theCountries = new ArrayList <>();。 在main方法中。

  • 您永遠不會在國家列表中添加新的“國家”對象。 因此,如果您已在main方法中聲明了Countrys,只需在“新國家(...)”之后使用“ theCountries.add(aCountries)”。

  • 您的seach方法(例如largestPop)將無法使用,因為它們永遠不會搜索“ theCountries” ArrayList的內容。 (“ i”變量只是在索引中進行迭代,但從未真正用於從此ArrayList獲取計數)。

  • 而且順便說一句,不需要System.exit(0)(暗含)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM