如何找到一个对象的长度

huangapple go评论114阅读模式
英文:

how to find length of an object

问题

以下是翻译好的部分:

我正在努力寻找消息对象的长度。给定的任务如下:练习模板带有一个预定义的 Message 类,可用于创建表示消息的对象。每条消息都有一个发送者和一些内容。

实现 MessagingService 类。该类必须有一个无参数的构造函数,并包含一个 Message 对象的列表。然后,向该类添加以下两个方法:

  1. public void add(Message message) - 将传递的消息作为参数添加到消息服务中只要消息内容最多为 280 个字符
  2. public ArrayList<Message> getMessages() - 返回添加到消息服务中的消息

提示!您可以使用与字符串关联的 length() 方法来找出字符串的长度。

我的工作如下:

  1. import java.util.ArrayList;
  2. public class MessagingService {
  3. ArrayList<Message> messages;
  4. public MessagingService(){
  5. this.messages = new ArrayList();
  6. }
  7. public void add(Message message){
  8. if(message.length() <= 280){
  9. messages.add(message);
  10. }
  11. }
  12. public ArrayList<Message> getMessages(){
  13. ArrayList<Message> mess = new ArrayList();
  14. for(Message m:messages){
  15. mess.add(m);
  16. }
  17. return mess;
  18. }
  19. }
  20. import java.util.Objects;
  21. public class Message {
  22. private String sender;
  23. private String content;
  24. public Message(String sender, String content) {
  25. this.sender = sender;
  26. this.content = content;
  27. }
  28. public String getSender() {
  29. return sender;
  30. }
  31. public String getContent() {
  32. return content;
  33. }
  34. public String toString() {
  35. return this.sender + ": " + this.content;
  36. }
  37. public int length(){
  38. return this.content.length();
  39. }
  40. @Override
  41. public boolean equals(Object obj) {
  42. if (this == obj) {
  43. return true;
  44. }
  45. if (obj == null) {
  46. return false;
  47. }
  48. if (getClass() != obj.getClass()) {
  49. return false;
  50. }
  51. final Message other = (Message) obj;
  52. if (!Objects.equals(this.sender, other.sender)) {
  53. return false;
  54. }
  55. if (!Objects.equals(this.content, other.content)) {
  56. return false;
  57. }
  58. return true;
  59. }
  60. }
英文:

I am struggling to find the length of a message object. The given task is as follow: The exercise template comes with a pre-defined Message class that can be used to create objects representing messages. Each message has a sender and some content.

Implement the MessagingService class. The class must have a parameterless constructor and contain a list of Message objects. After that, add the following two methods to the class:

public void add(Message message) - adds a message passed as a parameter to the messaging service as long as the message content is at most 280 characters long.
public ArrayList<Message> getMessages() - returns the messages added to the messaging service.
Tip! You can find out the length of the string using the length() method associated with the string.

What I have done is:

>
import java.util.ArrayList;

public class MessagingService {
ArrayList<Message> messages;

  1. public MessagingService(){
  2. this.messages = new ArrayList();
  3. }
  4. public void add(Message message){
  5. if(message.length() &lt;= 280){
  6. messages.add(message);
  7. }
  8. }
  9. public ArrayList&lt;Message&gt; getMessages(){
  10. ArrayList&lt;Message&gt; mess = new ArrayList();
  11. for(Message m:messages){
  12. mess.add(m);
  13. }
  14. return mess;
  15. }

}

import java.util.Objects;

public class Message {

  1. private String sender;
  2. private String content;
  3. public Message(String sender, String content) {
  4. this.sender = sender;
  5. this.content = content;
  6. }
  7. public String getSender() {
  8. return sender;
  9. }
  10. public String getContent() {
  11. return content;
  12. }
  13. public String toString() {
  14. return this.sender + &quot;: &quot; + this.content;
  15. }
  16. public int length(){
  17. return this.length();
  18. }
  19. // created using the &quot;insert code&quot; feature of NetBeans
  20. @Override
  21. public boolean equals(Object obj) {
  22. if (this == obj) {
  23. return true;
  24. }
  25. if (obj == null) {
  26. return false;
  27. }
  28. if (getClass() != obj.getClass()) {
  29. return false;
  30. }
  31. final Message other = (Message) obj;
  32. if (!Objects.equals(this.sender, other.sender)) {
  33. return false;
  34. }
  35. if (!Objects.equals(this.content, other.content)) {
  36. return false;
  37. }
  38. return true;
  39. }

}

Blockquote

答案1

得分: 1

在你的Message类内部,这是无意义的。它会抛出堆栈溢出异常:

  1. public int length(){
  2. return this.length();
  3. }

你可能需要的是:

  1. public int length(){
  2. return content.length();
  3. }
英文:

Inside your Message class, this is nonsense. It will throw stackoverflow exception:

  1. public int length(){
  2. return this.length();
  3. }

What you probably need here is:

  1. public int length(){
  2. return content.length();
  3. }

huangapple
  • 本文由 发表于 2020年4月6日 17:12:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/61056414.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定