首页 > 解决方案 > 为什么即使我的构造函数是公共的,我的构造函数也不可见的错误

问题描述

我正在做一个项目,从记事本中读取文本并将其内容存储在动态数组中,然后将列表显示给用户让他选择一行,然后将与所选区域相关的数据保存在二进制文件中,然后读取二进制文件并将其内容存储在“链表”类型的数据结构中,并将数据结构的内容保存到文本文件中。

我创建了 3 个类,其中两个是动态数组和链表。我想在第四个类中调用动态数组,但它说该数组不可见,即使我将其声明为公共,并且我的所有类都在同一个包中。


public class Array {
    
    private int length = DEFAULT_LENGTH;
    private int eltsNbr;
    public Country[] c;
    
    public static final int DEFAULT_LENGTH = 4;
    
    
    
        
        
    public Array(int length) {
        this.length = length;
        this.eltsNbr = 0;
        this.c= new Country[DEFAULT_LENGTH];
    }

    public Array() {
        this(DEFAULT_LENGTH);
        }
        
    public Array(Array a) {
        this(a.length);
        for (int i = 0; i < a.eltsNbr; i++)
            this.c[i] = a.c[i];
        this.eltsNbr = a.eltsNbr;
        }


    public int getLength() {
            return length;
        }


    public void setLength(int length) {
            this.length = length;
        }


    public Country[] getCountries() {
            return c;
        }


    public int getEmptyLength() {
            return this.length - this.eltsNbr;
            }
    
    public String toString() {
        String str = this.eltsNbr+"Countries"+"\n" ;

        if (this.isEmpty())
            return str;

        for (int i = 0; i < this.eltsNbr; i++)
            str += "Country [ "+" CountryName = " + c[i].getCountry() + " " + "code= "+c[i].getCode()+ " " + "region = " + c[i].getRegion() + " " + "population = " + c[i].getPopulation () + "]" + "\n";
        
        return str;
    }
    
        
    public boolean isFull() {
        return this.eltsNbr == this.length;
            }
    
        
    public boolean isEmpty() {
        return this.eltsNbr == 0;
            }
    

    public void extend (int additionalLength) {
        Country[] c1 = new Country[this.length + additionalLength];
            
        for(int i = 0; i < this.eltsNbr; i++)
            c1[i] = this.c[i];
        this.c = c1;
            
        this.length = this.length + additionalLength;
        }
        
    public boolean insertAtPosition(Country value, int pos) {
        if (pos > this.eltsNbr || pos < 0)
            return false;
            
        if (this.isFull())
            this.extend(DEFAULT_LENGTH) ;
            
            
        for (int i = this.eltsNbr; i > pos; i--)
                
            this.c[i] = this.c[i - 1];
            this.c[pos] = value;
            this.eltsNbr++;
            return true;
            }
        
        
    public boolean insertAtHead(Country value) {
        return this.insertAtPosition(value, 0);
            }

        
    public boolean deleteAtPosition(int pos) {
        if (this.isEmpty())
            return false;
            
        if (pos >= this.eltsNbr || pos < 0)
            return false;
            
        for (int i = pos; i < this.eltsNbr - 1; i++)
            this.c[i] = this.c[i + 1];
            this.eltsNbr--;
            return true;
            }


    public static void main(String[] args) throws IOException {
//      

    }

}
    

    public static void main(String[] args) throws IOException{
        
        Array a1 = new Array();
    }

}

这是一个屏幕截图:
在此处输入图像描述

这是国家类:

import java.io.IOException;

public class Country {

    private String country; 
    private String code; 
    private String region;
    private int population;
    
    public static final String DEFAULT_COUNTRY = "Lebanon";
    public static final String DEFAULT_CODE = "LBN" ;
    public static final String DEFAULT_REGION = "Middle East & North Africa";
    public static final int DEFAULT_POPULATION = 6855713;

    
    
    public Country(String country, String code, String region, int population) {
        super();
        this.country = country;
        this.code = code;
        this.region = region;
        this.population = population;
    }

     public Country() {
        this(DEFAULT_COUNTRY, DEFAULT_CODE, DEFAULT_REGION, DEFAULT_POPULATION);
    }
    
     public Country(Country a) {
        this(a.country, a.code, a.region, a.population);
    }
    
    

    public String getCountry() {
        return country;
    }

    public void setCountry(String country) {
        this.country = country;
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public String getRegion() {
        return region;
    }

    public void setRegion(String region) {
        this.region = region;
    }

    public int getPopulation() {
        return population;
    }

    public void setPopulation(int population) {
        this.population = population;
    }
    
    

    @Override
    public String toString() {
        return "Country [country=" + country + ", code=" + code + ", region=" + region + ", population=" + population
                + "]";
    }
    
    
    
    

    public static void main(String[] args)   {
        
    }
}

这是链表类:

    
    class Element{
        Country data;
        Element next;
        Element previous;
        
        Element (Country c){
            data = c;
            next = null;
        }
        
    }
    
    private Element head = null;
    private Element rear = null;
    private int length = 0;
    
    
    public boolean isEmpty() {
        return this.length == 0;
    }
    
    
    
    public LinkedList() {
        this.head = null;
        this.rear = null;
        this.length = 0;
    }
    
    public LinkedList(LinkedList dList) {
        this();
        
        if(dList.isEmpty())
            return;
        
        Element cur = dList.head;
        Element tmp = new Element(cur.data);
        head = rear = tmp;
        cur = cur.next;
        length++;
        while(cur != null) {
            tmp = new Element(cur.data);
            rear.next = tmp;
            tmp.previous = rear;
            rear = tmp;
            cur = cur.next;
            length++;
        }
    }
    
    
    public int getLength() {
        return this.getLength();
    }
    
    

    public String toString() {
        Element cur = this.head;
        String str = this.length+"Countries"+"\n" ;
        if (this.isEmpty())
            return str;
        while (cur != null) {

            str += "Country " + cur.data.getCountry() + " " + "Code = " + cur.data.getCode()+ " " + "Region " + cur.data.getRegion() + " " + "Population = "+ cur.data.getPopulation() + " | ";
            cur = cur.next;
        }

        return str;
    }
    
    
    public void insertAtHead(Country value) {
        Element tmp = new Element(value);
        
        // case on an empty list
        if(this.isEmpty()) {
            head = rear = tmp;
            head.previous = null;
            rear.next = null;
        }else {
        
        //case of a non-empty list
            tmp.next = head;
            head.previous = tmp;
            tmp.previous = null;
            head = tmp;
            
            }
        this.length++;
    }
    
    
    
    public void insertAtRear(Country value) {
        Element tmp = new Element(value);
        
        // case on an empty list
        if(this.isEmpty()) {
            head = rear = tmp;
            head.previous = null;
            rear.next = null;
        }else {
        
        //case of a non-empty list
            tmp.previous = rear;
            rear.next = tmp;
            tmp.next = null;
            rear = tmp;
            
            }
        this.length++;
        
    }
    
    
    public void delete(Country value) {
        Element cur = this.head;
        
        if(this.isEmpty())
            return;
        
        //case of deleting the head
        if(this.head.data == value) {
            this.head.next = this.head;
        }
        
        //case of deleting the rear
        if(this.rear.data == value) {
            this.rear.previous = this.rear;
        }
        
        //general case
        while(cur != null && cur.data != value) {
            cur = cur.next;
        }
        
        cur.previous.next = cur.next;
        cur.next.previous = cur.previous;
        
    }
    
    public void deleteAllElements() {
        this.length = 0;
        this.head = null;
        this.rear = null;
    }

}

标签: javaarrayslinked-listdoubly-linked-list

解决方案


问题是您导入了不正确的类。请在测试类中查看您的导入语句。


import java.lang.reflect.Array;

请删除该导入语句。那么你很高兴。


推荐阅读