printing data of specific element from array in ja

2019-09-22 06:43发布

This question already has an answer here:

I have an array of a class type which stores videos (not real ones just objects). Each video has a title and an author. The videos have their data set using set methods like these:

 public class Clip {

 private String title;
 private String author;

     public void setTitle (String s1) {

        title = s1;

    }

     public void setAuthor (String s2) {

        title = s2;

    }

I then have a different class which creates the array and uses these set methods to enter data. The program then asks the user to select an element from the array, for example the user selects element [3]. Now the program must print the title and author of that element(video).

This is the part I am not sure how to do? Can I please have some help?

8条回答
再贱就再见
2楼-- · 2019-09-22 06:47

Override toString() method in Clip class as follows,

 @override
   public String toString(){ 

    return "Title: "+ title +"  ,author"+author;

   }

and invocation code invoke following statement,

System.out.println(element[3]);
查看更多
放我归山
3楼-- · 2019-09-22 06:50

Your implementation of Clip class is wrong. It should be something like this:

public class Clip {
        private String title;
        private String author;

        public Clip(String title, String author) {
            this.title = title;
            this.author = author;
        }

        public String getTitle() {
            return title;
        }

        public void setTitle(String title) {
            this.title = title;
        }

        public String getAuthor() {
            return author;
        }

        public void setAuthor(String author) {
            this.author = author;
        }
    }

And you can retrieve your objects from array like this:

@Test
    public void testClipArray() throws Exception {
        // Lets assume our array contains 2 elements
        Clip[] clipArray = new Clip[2];
        clipArray[0] = new Clip("First", "Clip");
        clipArray[1] = new Clip("Second", "Clip");

        // Lets retrieve 2nd object (with index: 1)
        Clip secondObject = clipArray[1];

        System.out.println(secondObject.getAuthor());
        System.out.println(secondObject.getTitle());
    }
查看更多
一纸荒年 Trace。
4楼-- · 2019-09-22 06:51

Override toString() method of Clip class. For example,

public class Clip {

    private String title;
    private String author;

    public void setTitle (String s1) {
        title = s1;
    }

    public void setAuthor (String s2) {
        title = s2;
    }

    @Override
    public String toString() {
        return title + " by " + author;
    }
}

Then print the array of clips as follows:

for (Clip aClip : arrayOfClips) {
    System.out.println(aClip);
}
查看更多
看我几分像从前
5楼-- · 2019-09-22 06:53

Override toString() then simply call

System.out.println(element[3])

If you need help creating the toString method eclipse can show you how it's done.

Press Alt+Shift+S then S , or right click -> Source -> generate toString()

查看更多
对你真心纯属浪费
6楼-- · 2019-09-22 06:57

Please override toString () of Clip class as below

@Override
public String toString() {
     return "Title:"+ this.s1 + "  Author:" + this.s2;
}


Then print the Title and Author by using the below code.
System.out.println(objectClip.toString());
查看更多
别忘想泡老子
7楼-- · 2019-09-22 06:59
public class Clip {

private String title;
private String author;

 public void setTitle (String s1) {

    title = s1;

}

 public void setAuthor (String s2) {

    author = s2;

}

@Override
public String toString(){
    return author + ": " + title;
}
查看更多
登录 后发表回答