【问题标题】:Attempt to store wrong type in array of objects尝试在对象数组中存储错误的类型
【发布时间】:2016-10-29 07:33:57
【问题描述】:

我收到了我发现的这个错误 - 尝试在对象数组中存储错误的类型。文件编译,我需要将正在读入的文件存储到 Animals[] 数组中。

这是整个文件:

import java.util.*;                                                                                              
import java.io.BufferedReader;                                                                                   
import java.io.FileReader;                                                                                       
import java.io.IOException;                                                                                      
import java.io.InputStreamReader;                                                                                
import java.io.FileInputStream;                                                                                  
public class Zoo                                                                                                 
{                                                                                                                                                                                                     
private Animals[] animals;                                                                                   
int count;                                                                                                   
public final int Maximum_Count = 20;                                                                         
public boolean constructed = false;                                                                          
                                                                                                                                                            //Default Constructor                                                          
public Zoo()                                                                                                 
{                                                                                                            
    count = 0;                                                                                               
    animals = new Animals[Maximum_Count];                                                                    
}                                                                                                            
                                                                                //Alternate Constructor                   
public Zoo(int inCount, Animals[] inAnimals)                                                                 
{                                                                                                            
    if(validateCount(inCount))                                                                               
    {                                                                                                        
        animals = new Animals[Maximum_Count];                   //Probably Wont have to validate Count       
        for(int ii = 0; ii < Maximum_Count; ii++)                                                            
        {                                                                                                    
            inAnimals[ii] = new Animals(inAnimals[ii]);                                                      
        }                                                                                                    
    }                                                                                                        
}                                                                                                            
                                                                            //Copy Constructor                                                                                                            
public Zoo(Zoo inZoo)                                                                                        
{                                                                                                            
    count = inZoo.getCount();                                                                                
    animals = inZoo.getAnimals();                                                                            
}                                                                                                            

//Mutators                                                                                                   


public void setCount(int inCount)                                                                            
{                                                                                                            
    if(validateCount(inCount))                                                                               
    {                                                                                                        
        count = inCount;                                                                                     
    }                                                                                                        
    else                                                                                                     
    {                                                                                                        
        throw new IllegalArgumentException("Invalid Count");                                                 
    }                                                                                                        
}                                                                                                            

public void setAnimals(Animals[] inAnimals)                                                                  
{                                                                                                            
    if(inAnimals == null)                                                                                    
    {                                                                                                        
        throw new IllegalArgumentException("Animals can not be found, array is empty.");                     
    }                                                                                                        
    else                                                                                                     
    {                                                                                                        
        animals = new Animals[inAnimals.length];                                                             
        for(int ii = 0; ii < inAnimals.length; ii++)                                                         
            {                                                                                                
                animals[ii] = new Animals(inAnimals[ii]);                                                    
            }                                                                                                
    }                                                                                                        
}                                                                                                            

public int getCount()                                                                                        
{                                                                                                            
    return count;                                                                                            
}                                                                                                            

public Animals[] getAnimals()                                                                                
{                                                                                                            
    Animals[] animalsCopy;                                                                                   
    animalsCopy = new Animals[animals.length];                                                               
    for(int ii = 0; ii < animals.length; ii++)                                                               
    {                                                                                                        
         animalsCopy[ii] = new Animals(animals[ii]);                                                         
    }                                                                                                        
    return animalsCopy;                                                                                      
}                                                                                                            


public boolean equals(Object inObject)                                                                       
{                                                                                                            
    Zoo inZoo;                                                                                               
    boolean same = false;                                                                                    
    if(inObject instanceof Zoo)                                                                              
    {                                                                                                        
        inZoo = (Zoo)inObject;                                                                               
        if(count == inZoo.getCount())                                                                        
        {                                                                                                    
            if(sameAs(animals,inZoo.getAnimals()))                                                           
            {                                                                                                
                same = true;                                                                                 
            }                                                                                                
        }                                                                                                    
    }                                                                                                        
    return same;                                                                                             
}                                                                                                            

public String toString()                                                                                     
{                                                                                                            
    String outString = "Count: " + count;                                                                    
    for(int ii = 0; ii < animals.length; ii++)                                                               
    {                                                                                                        
         outString = outString + ("Animals" + ii + ": " + animals[ii].toString());                           
    }                                                                                                        
    return outString;                                                                                        
}                                                                                                            

public void addFlying()                                                                                      
{                                                                                                            
    Scanner sc = new Scanner(System.in);                                                                     
    String species;                                                                                          
    int numWings;                                                                                            
    double mass;                                                                                             
    System.out.println("Please enter the Species of the Flying Animal.");                                    
    species = sc.nextLine();                                                                                 
    System.out.println("Please enter the Mass of the Flying Animal.");                                       
    mass = sc.nextDouble();                                                                                  
    System.out.println("Please enter the Number of Wings the Flying Animal has.");                           
    numWings = sc.nextInt();                                                                                 
    Flying temp = new Flying(species,mass,numWings);                                                         
    animals[count] = temp;                                                                                   
    System.out.println("Animal has been added to the Zoo.");                                                 
    count++;                                                                                                 
}                                                                                                      
public void addTerrestrial()                                                                                 
{                                                                                                            
    Scanner sc = new Scanner(System.in);                                                                     
    String species;                                                                                          
    int numLegs;                                                                                             
    double mass;                                                                                             
    System.out.println("Please enter the Species of the Terrestrial Animal.");                               
    species = sc.nextLine();                                                                                 
    System.out.println("Please enter the Mass of the Terrestrial Animal.");                                  
    mass = sc.nextDouble();                                                                                  
    System.out.println("Please enter the Number of Legs that the Terrestrial Animal has.");                  
    numLegs = sc.nextInt();                                                                                  
    Terrestrial temp = new Terrestrial(numLegs,mass,species);                                                
    animals[count] = temp;                                                                                   
    System.out.println("Animal has been added to the Zoo.");                                                 
    count++;                                                                                                 
}                                                                                 
public void addAquatic()                                                                                     
{                                                                                                            
    Scanner sc = new Scanner(System.in);                                                                     
    String species;                                                                                          
    int numFins;                                                                                             
    double mass;                                                                                             
    System.out.println("Please enter the Species of the Aquatic Animal.");                                   
    species = sc.nextLine();                                                                                 
    System.out.println("Please enter the Mass of the Aquatic Animal.");                                      
    mass = sc.nextDouble();                                                                                  
    System.out.println("Please enter the Number of Fins that the Aquatic Animal has.");                      
    numFins = sc.nextInt();                                                                                  
    Aquatic temp = new Aquatic(mass,species,numFins);                                                        
    animals[count] = temp;                                                                                   
    System.out.println("Animal has been added to the Zoo.");                                                 
    count++;                                                                                                 
}                                                                           
public void displayAnimals()                                                                                 
{                                                                                                            
    for(int i=0; i<count; i++)                                                                               
    {                                                                                                        
        if(animals[i] instanceof Flying)                                                                     
        {                                                                                                    
            Flying test1 = new Flying((Flying)animals[i]);                                                   
            System.out.println(test1.toString());                                                            
        }                                                                                                    
        if(animals[i] instanceof Terrestrial)                                                                
        {                                                                                                    
            Terrestrial test2 = new Terrestrial((Terrestrial)animals[i]);                                    
            System.out.println(test2.toString());                                                            
        }                                                                                                    
        if(animals[i] instanceof Aquatic)                                                                    
        {                                                                                                    
            Aquatic test3 = new Aquatic((Aquatic)animals[i]);                                                
            System.out.println(test3.toString());                                                            
        }                                                                                                    
    }                                                                                                        
}                                                                                                                           
public Animals[] readFile()                                                                                  
{                                                                                                            
    try                                                                                                      
    {                                                                                                        
        Scanner sc = new Scanner(System.in);                                                                 
        System.out.println("Enter the file name.");                                                          
        String fileName = sc.nextLine();                                                                     
        String string;                                                                                       
        BufferedReader bufferedReader = new BufferedReader(new FileReader(fileName));                        
        List<String> list = new ArrayList<String>();                                                         
        for(int a = 0; a<animals.length; a++)                                                                
        {                                                                                                    
            while((string = bufferedReader.readLine()) != null)                                              
            {                                                                                                
                list.add(string);                                                                            
            }                                                                                                
            animals = list.toArray(new Animals[a]);                                                          
        }                                                                                                    
    }                                                                                                        

    catch(IOException e)                                                                                     
    {                                                                                                        
        System.out.println(e.getMessage());                                                                  
    }                                                                                                        
    catch(ArrayStoreException e2)                                                                            
    {                                                                                                        
        System.out.println("Attempt to Store wrong type of object into an array of objects.");               
    }                                                                                                        
    return animals;                                                                                          
}                                                                                                                                                  
//Private Submodules                                                                      
private boolean sameAs(Object[] array1, Object[] array2)                                                     
{                                                                                                            
    boolean sameAs = true;                                                                                   
    if(array1.length != array2.length)                                                                       
    {                                                                                                        
        sameAs = false;                                                                                      
    }                                                                                                        
    else                                                                                                     
    {                                                                                                        
        int count = 0;                                                                                       
        do                                                                                                   
        {                                                                                                    
            sameAs = array1[count].equals(array2[count]);                                                    
            count++;                                                                                         
        }                                                                                                    
        while(sameAs && (count < array1.length));                                                            
    }                                                                                                        
    return sameAs;                                                                                           
}                                                                                                            


private boolean validateCount(int inCount)                                                                   
{                                                                                                            
    return((inCount > 0) && (inCount <= 20));                                                                
}                                                                                                            
}           

这是我遇到问题的 readFile 子模块。其他一切正常,文件编译。 欢迎任何解决方案,我已经绞尽脑汁好几个小时了。

【问题讨论】:

    标签: java arrays io


    【解决方案1】:

    您正在尝试将List&lt;String&gt; 转换为Animals 的数组。 String 不是 Animals,所以这永远行不通。这就是你的错误告诉你的。

    为了能够为您提供有关如何解决此问题的更多指导,我需要查看您的 Animals 类以及输入文件的结构。您还没有在此处显示其中任何一个。

    【讨论】:

    • F,Condor,15.95,4 F,Crested Pidgeon,0.49,2 T,Rhinoceros,1550,4 这是输入文件。 A,Great White Shark,1800,31,3 T,Shrew,0.28,2 - 格式很难在堆栈上完成。
    • 我不能发布动物类,因为 Stack 限制了我的字符。基本上,动物阵列有 3 种类型的动物,陆地、飞行和水生。每种动物类型都有一个物种、质量和翅膀/腿/鳍的数量。
    • 嗯,最重要的是,您需要编写一些代码将该文件的一行转换为Animals 对象。 Java 不会为您神奇地将 String 转换为 Animals
    猜你喜欢
    • 1970-01-01
    • 2017-09-28
    • 1970-01-01
    • 2021-04-20
    • 2020-07-20
    • 1970-01-01
    • 1970-01-01
    • 2014-04-12
    • 1970-01-01
    相关资源
    最近更新 更多