Skip to content

Why do I have an error that my constructor is not visible even though my constructor is public [closed]

I am working on a project to read a text from a note pad and store its content in a dynamic array, then display the list to the user for him to choose a line, then saving the data related to the chosen region in a binary file, then read the binary file and store its content in a data structure of type “Linked List” and save the content of the data structure into a text file.

I created 3 classes two of them are the dynamic array and the linked list. I want to call the dynamic array in a fourth class but it says that the array is not visible even though I declared it as public, and all of my classes are in the same package.

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

}

This is a screenshot:
enter image description here

This is the Country class:

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)   {
        
    }
}

this is the linked list class:

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

}

Answer

The problem is you have imported an incorrect class. Please look at your import statement in the class Testing.

import java.lang.reflect.Array;

please remove that import statement.Then you are good to go.