计算Java Array对象内属性的出现次数

时间:2017-02-11 01:09:43

标签: java arrays object

我创建了一个包含25个Flower对象的数组。每个花卉对象都有花名(String),花色(String),荆棘(boolean)和花香(String)。这些属性由'Flower'类处理。我已粘贴这两个类,以防错误是由任何一个类引起的。当菜单提示输入信息时,用户输入鲜花的所有属性。在用户输入他们想要的所有花朵之后,我需要能够打印出整个阵列以及每个花朵中有多少花朵的计数器。例如,如果用户放入10朵花,并且有3朵玫瑰,2朵莉莉,3朵蒲公英和2朵兰花,我需要打印整个阵列,然后打印每朵花的数量。显示的格式为:

花名:玫瑰花颜色:红花有刺:真花香:甜 罗斯 - 3 莉莉 - 3 蒲公英 - 3 兰花 - 2

我能够如图所示打印出数组,但无法使count变量正常工作。我不需要对这个数组进行排序。

我在OutOfBounds错误中遇到的另一个问题。在遇到这个错误之前,我只能放入24朵花。第25朵花触发了它。我认为这是由addFlower索引计数器覆盖的,但显然,我是不正确的。

这个赋值不允许使用ArrayList,这会使这更加简单。我们还没有探讨错误处理。

目前的代码是:

package assignment2;
import java.util.Scanner;

public class Assignment2 
{
    public static void main(String[] args) 
    {
        new Assignment2();
    }

    public Assignment2() 
    {
        Scanner input = new Scanner(System.in);
        Flower flowerPack[] = new Flower[25];

        System.out.println("Welcome to my flower pack interface.");
        System.out.println("Please select a number from the options below");
        System.out.println("");

        while (true) 
        {
            // Give the user a list of their options
            System.out.println("1: Add an item to the pack.");
            System.out.println("2: Remove an item from the pack.");
            System.out.println("3: Search for a flower.");
            System.out.println("4: Display the flowers in the pack.");
            System.out.println("0: Exit the flower pack interfact.");

            // Get the user input
            int userChoice = input.nextInt();

            switch (userChoice) 
            {
            case 1:
                addFlower(flowerPack);
                break;
            case 2:
                removeFlower(flowerPack);
                break;
            case 3:
                searchFlowers(flowerPack);
                break;
            case 4:
                displayFlowers(flowerPack);
                break;
            case 0:
                System.out.println("Thank you for using the flower pack interface. See you again soon!");
                input.close();
                System.exit(0);             
            }
        }   
    }


    private void addFlower(Flower flowerPack[]) 
    {       
        String flowerName; // Type of flower
        String flowerColor; // Color of the flower
        Boolean hasThorns = false; // Have thorns?
        String flowerScent;   // Smell of the flower

        int index = 0;
        Scanner input = new Scanner(System.in);
        System.out.println("What is the name of flower is it?");
        flowerName = input.nextLine();
        System.out.println("What color is the flower?");
        flowerColor = input.nextLine();
        System.out.println("Does the flower have thorns?");
        System.out.println("Choose 1 for yes, 2 for no");
        int thorns = input.nextInt();
        if(thorns == 1)
        {
            hasThorns = true;
        }
        input.nextLine();
        System.out.println("What scent does the flower have?");
        flowerScent = input.nextLine();        

        Flower fl1 = new Flower(flowerName, flowerColor, hasThorns, flowerScent);  

        for(int i = 0; i < flowerPack.length; i++)
        {
            if(flowerPack[i] != null)
            {
                index++;            
                if(index == flowerPack.length)
                {
                    System.out.println("The pack is full");
                }               
            }
            else
            {
                flowerPack[i] = fl1;  
                break;
            }                       
        }
    }

    private void removeFlower(Flower flowerPack[]) 
    {       
        Scanner input = new Scanner(System.in);
        System.out.println("What student do you want to remove?");
        displayFlowers(flowerPack);
        System.out.println("Choose 1 for the first flower, 2 for the second, etc" );
        int index = input.nextInt();
        index = index - 1;

        for (int i = 0; i < flowerPack.length - 1; i++) 
        {
            if(flowerPack[i] != null && flowerPack[i].equals(flowerPack[index]))
            {               
                flowerPack[i] = flowerPack[i + 1];                
            }
        } 
    }

    private void searchFlowers(Flower flowerPack[]) 
    {
        Scanner input = new Scanner(System.in);
        String name;
        System.out.println("What flower would you like to search for?");
        name = input.nextLine();     
        boolean found = false;

        for (int i = 0; i < flowerPack.length; i++) 
        {
            if (flowerPack[i].getFlowerName().equalsIgnoreCase(name)) 
            {
                found = true;
                break;
            }
        }
        if (found) 
        {
            System.out.println("We found your flower.");
        } 
        else 
        {
            System.out.println("That flower was not found.");
        }        
    }

    private void displayFlowers(Flower flowerPack[])
    {
        int count = 1;

        for(int i = 0; i < flowerPack.length; i++)
        {
            if (flowerPack[i] != null)
            {           
                if (flowerPack[i].equals(flowerPack[i+1]))
                {               
                    count++;
                } 
                else
                {
                    System.out.println(flowerPack[i]);
                    count = 1;
                }
            }
            else
            {
                if (flowerPack[i] == null)
                {
                    break;  
                }
            }
        }           
    }   
}

花类如下。     包分配2;

public class Flower 
{   
    @Override
    public String toString() 
    {
        return "Flower name: " + this.getFlowerName() +  "\t" +          
               "Flower color: " + this.getFlowerColor() + "\t" +
               "Flower has thorns: " + this.getHasThorns() + "\t" +
               "Flower scent: " + this.getFlowerScent() + "\t" ;
    }   

    private String flowerName;
    private String flowerColor;
    private Boolean hasThorns;
    private String flowerScent;
    Flower(String flowerName, String flowerColor, Boolean hasThorns, String flowerScent) 
    {
        this.flowerName = flowerName;
        this.flowerColor = flowerColor;
        this.hasThorns = hasThorns;
        this.flowerScent = flowerScent;
    }   

    String getFlowerName() 
    {
        return flowerName;
    }

    private void setFlowerName(String flowerName)
    {
        this.flowerName = flowerName;
    }

    private String getFlowerColor() 
    {
        return flowerColor;
    }

    private void setFlowerColor()
    {
        this.flowerColor = flowerColor;
    }

    private Boolean getHasThorns() 
    {
        return hasThorns;
    }

    private void setHasThorns()
    {
        this.hasThorns = hasThorns;
    }

    private String getFlowerScent() 
    {
        return flowerScent;
    }

    private void setFlowerScent()
    {
        this.flowerScent = flowerScent;
    }
}

1 个答案:

答案 0 :(得分:0)

private void displayFlowers(Flower flowerPack[])
    {       
        String[] usedNames = new String[flowerPack.length];
        int[] nameCounts = new int[flowerPack.length];
        int usedNamesCount = 0;

        for (int i = 0; i < flowerPack.length; i++) 
        {
            Flower flower = flowerPack[i];
            if (flower == null) 
            { 
                continue; 
            }                
            int nameIndex = -1;
            for (int j = 0; j < usedNamesCount; j++) 
            {
                String usedName = usedNames[j];                    
                if (flower.getFlowerName().equals(usedName)) 
                {
                    nameIndex = j;
                    break;
                }
            }                
            if (nameIndex != -1) 
            {
                nameCounts[nameIndex] += 1;
            } 
            else 
            {
                usedNames[usedNamesCount] = flower.getFlowerName();
                nameCounts[usedNamesCount] += 1;
                usedNamesCount++;
            }
        }            
        for (int i = 0; i < usedNamesCount; i++) 
        {
            System.out.println(usedNames[i] + "s - " + nameCounts[i]);
        }
    }