Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
518 views
in Technique[技术] by (71.8m points)

Java (Objects) - Cannot invoke add(Star) on the array type Star[] - I have no clue where my mistake is

My exercise was to create an add-method where I can add a Star to a Star[], but like the title of my question says, I have literally no clue where my mistake in the code is. I have been looking for it for over an hour now, please take a look and tell me what I did wrong. Thanks! :)

public class Star {

private String name;
private String id;
private double distance;
private double apparentMagnitude;
private String type;


// Default-Constructor
public Star() {
    
}


// Custom-Constructor
public Star(String name, String id) {
    this.name = name;
    this.id = id;
}




public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public String getId() {
    return id;
}

public void setId(String id) {
    this.id = id;
}

public double getDistance() {
    return distance;
}

public void setDistance(double distance) {
    this.distance = distance;
}

public double getApparentMagnitude() {
    return apparentMagnitude;
}

public void setApparentMagnitude(double apparentMagnitude) {
    this.apparentMagnitude = apparentMagnitude;
}

public String getType() {
    return type;
}

public void setType(String type) {
    this.type = type;
}

}

public class StarsDatabase {

private Star[] moreStars = new Star[0];




/*public void add(Star other) {
    Star[] hm = new Star[moreStars.length + 1];
    
    int test = 1;
    for (Star yes : moreStars) {
        hm[test] = yes;
        test++;
    }
    hm[test] = other;
    
}*/

public void add(Star other) {
    Star[] yes = new Star[moreStars.length + 1];
    
    for (int i = 0; i > moreStars.length - 1; i++) {
        yes[i] = moreStars[i];
    }
    yes[yes.length] = other;
    moreStars = yes;
}


public Star[] getMoreStars() {
    return moreStars;
}

public void setMoreStars(Star[] moreStars) {
    this.moreStars = moreStars;
}   

}

public class TestStar {

public static void main(String[] args) {

    Star first = new Star("Sirius", "TYC 5949-2777-1");
    Star second = new Star("Alpha Centauri", "TYC 9007-5849-1");
    Star third = new Star("Rigel", "TYC 5331-1752-1");
    Star fourth = new Star("Almaaz", "TYC 2907-1275-1");
    Star fifth = new Star("Luhman 16", "WISE J1049-5319A");
    
    
    
    System.out.println(first.getName());
    System.out.println(second.getName());
    
    
    Star[] okay = new Star[3];
    Star hihi = new Star("banana", "ABCDE");
    
    okay.add(hihi);
    // This part of the code is showing me the error, but I have no clue how to fix it.
    
}

}

question from:https://stackoverflow.com/questions/65853435/java-objects-cannot-invoke-addstar-on-the-array-type-star-i-have-no-cl

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...