可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
This question already has an answer here:
-
printing out data of array element in java
4 answers
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?
回答1:
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());
}
回答2:
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()
回答3:
Basically you have two options:
- Create getters in the Video-Class, e.g.
public String getTitle() { return title; }
Override the toString
function to output the value you want, e.g.
@Override
public String toString() {
return "Author: " + author + ", Title: " + title";
}
回答4:
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;
}
回答5:
Override toString method of Clip. Every invocation of print* (e.g. println) method would call toString method of an object.
@Override
public String toString() {
return "[Title: " + this.title +", Author: " + this.author + "]";
}
Example after overriding method:
Clip clip1 = new Clip();
Clip clip2 = new Clip();
Clip[] clipArray = {clip1, clip2};
for (Clip clip : clipArray) {
System.out.println(clip);
}
回答6:
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:
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);
}
回答8:
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]);