如何找到一个对象的长度

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

how to find length of an object

问题

以下是翻译好的部分:

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

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

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

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

我的工作如下:

import java.util.ArrayList;

public class MessagingService {
    ArrayList<Message> messages;
    
    public MessagingService(){
        this.messages = new ArrayList();
    }
    
    public void add(Message message){
        if(message.length() <= 280){
            messages.add(message);
        }
    }
 
    public ArrayList<Message> getMessages(){
        ArrayList<Message> mess = new ArrayList();
        for(Message m:messages){
            mess.add(m);
        }
        return mess;
    }
}

import java.util.Objects;

public class Message {
    private String sender;
    private String content;

    public Message(String sender, String content) {
        this.sender = sender;
        this.content = content;
    }

    public String getSender() {
        return sender;
    }

    public String getContent() {
        return content;
    }

    public String toString() {
        return this.sender + ": " + this.content;
    }

    public int length(){
        return this.content.length();
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj) {
            return true;
        }
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        final Message other = (Message) obj;
        if (!Objects.equals(this.sender, other.sender)) {
            return false;
        }
        if (!Objects.equals(this.content, other.content)) {
            return false;
        }
        return true;
    }
}
英文:

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;

public MessagingService(){
this.messages = new ArrayList();
}
public void add(Message message){
if(message.length() &lt;= 280){
messages.add(message);
}
}
public ArrayList&lt;Message&gt; getMessages(){
ArrayList&lt;Message&gt; mess = new ArrayList();
for(Message m:messages){
mess.add(m);
}
return mess;
}

}

import java.util.Objects;

public class Message {

private String sender;
private String content;
public Message(String sender, String content) {
this.sender = sender;
this.content = content;
}
public String getSender() {
return sender;
}
public String getContent() {
return content;
}
public String toString() {
return this.sender + &quot;: &quot; + this.content;
}
public int length(){
return this.length();
}
// created using the &quot;insert code&quot; feature of NetBeans
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
final Message other = (Message) obj;
if (!Objects.equals(this.sender, other.sender)) {
return false;
}
if (!Objects.equals(this.content, other.content)) {
return false;
}
return true;
}

}

Blockquote

答案1

得分: 1

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

public int length(){
    return this.length();
}

你可能需要的是:

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

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

public int length(){
return this.length();
}

What you probably need here is:

 public int length(){
return content.length();
}

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:

确定