在ArrayList中添加对象

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

adding objects in an arrayList

问题

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 && !(this.messages.contains(message))) {
            this.messages.add(message);
        }
    }

    public ArrayList<Message> getMessages() {
        ArrayList<Message> mess = new ArrayList();
        for (Message m : messages) {
            mess.add(m);
        }
        return mess;
    }

    public static void main(String[] args) {

        // Try out your class here
        MessagingService ms = new MessagingService();
        System.out.println(ms.getMessages());
        Message m = new Message("sender", "message");
        ms.add(asdf);
        System.out.println(ms.getMessages());
    }

}
英文:

I am a student taking a course in object oriented programming. I am struggling to understand objects anybody here knows about a book or webside where I can learn it?

I have been giving this exercise, and I have no clue why I can't add a message to my arraylist.
What am I doing wrong?

import java.util.ArrayList;

public class MessagingService {
    ArrayList&lt;Message&gt; messages;
    
    
    public MessagingService(){
        this.messages = new ArrayList();
    }
    public void add(Message message){
        if(message.length() &gt;=280 &amp;&amp; !(this.messages.contains(message))){
            this.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;
    }
    public static void main(String[] args) {

        // Try out your class here
        MessagingService ms = new MessagingService();
        System.out.println(ms.getMessages());
        Message m = new Message(&quot;sender&quot;, &quot;message&quot;);
        ms.add(asdf);
        System.out.println(ms.getMessages());
    }
    
}

答案1

得分: 2

你好,如果你询问推荐阅读的好文章,我可以推荐给你这篇:
https://www.freecodecamp.org/news/object-oriented-programming-concepts-21bb035f7260/

另外,我建议你阅读一下关于Java中的集合(Collections)部分,因为你在这里所做的:

!(this.messages.contains(message))

只是为了防止重复添加元素到你的列表中,所以也许你应该考虑使用Set集合?除非你关心对象添加的顺序(ArrayList是有序的,Set不是)。正如你在这个简单的例子中可以看到的,这里有许多重要的特性,有时候选择正确的集合类型可以简化代码的编写过程。

之前的回答推荐了一些书籍给你。个人而言,我觉得作为初学者购买书籍可能会有些无聊,所以也许你可以在Udemy上找一个好的课程,以更互动的方式学习?

https://www.udemy.com/course/java-the-complete-java-developer-course/

我认为这门课程非常适合初学者,而且很有趣。

祝一切顺利。

英文:

Hello if you're asking about good articles to read I can recommend you this:
https://www.freecodecamp.org/news/object-oriented-programming-concepts-21bb035f7260/

also I would recommend you to read about Collections in Java, because what you are doing here:

!(this.messages.contains(message))

is just preventing for adding duplicates to your list, so maybe you should reconsider and simply use Set Collection? Unless order in which objects are added is important to you (ArrayList is ordered, Set isn't). As you can see in this simple example there are many important features here and sometimes choosing correct Collection can simplify process of creating the code.

Previous answers recommend you some books. I personally find it boring when you are beginner so instead of buying book maybe just find good course on Udemy and learn in more interactive way?

https://www.udemy.com/course/java-the-complete-java-developer-course/

This is the one I think is very good and fun for beginners.

Cheers and wish you best.

答案2

得分: 1

我认为问题在于当您将 Message 对象添加到您的 ArrayList 时,消息必须大于或等于 280 个字符,当然您的 m 消息不会被添加到 ArrayList 中,因为它包含的字符少于 280 个。

您应该更改这段代码

public void add(Message message){
    if(message.length() >= 280 && !(this.messages.contains(message))){
        this.messages.add(message);
    }
}

或者您可以尝试使用超过 280 个字符的字符串。

要学习 Java 语言,有很多资源可供选择,只需在 Google 上输入“Java Introduction”,您会找到许多学习 Java 的资源。至于书籍,我建议您阅读 O'Reilly 出版的《Head First Java》一书。

英文:

I think the problem is when you will add Message object to your ArrayList the message must contain greater than or equal 280, and of course your m message will not added to your ArrayList because it contain less than 280 character.

You should change this code

public void add(Message message){
    if(message.length() &gt;= 280 &amp;&amp; !(this.messages.contains(message))){
        this.messages.add(message);
    }
}

Or you can try using the String with more than 280 character.

For learning java language it's very lot of resources out there, just type "Java Introduction" on Google and you'll find a lot of resources you to learn Java. And for book I will recommend you to read Book "Head First Java" By O'Reilly

答案3

得分: 1

首先,你的代码无法编译(我敢打赌你已经注意到了)。

其次,你必须理解,当你想要将一条消息添加到你的 ArrayList 时,你试图添加的消息必须符合你在 if 语句中检查的条件,例如:消息的长度应该超过280个字符,并且 messagesArrayList 尚未包含你试图添加的消息。

如果你用以下代码替换这部分代码:

Message m = new Message("sender", "biUHobB2xW4Aed9jc5ZicRtgO1R2MUQGJeVHE3CBr0BxuSImM9E4ytkyeAKR22bEheJJIJ7PxzIsUjKYwsBKdYJeDA8VgKduTNJ0ofH66x3v20j3JnXZanWP0aRuxJmBatfmuEsBur8ald0eW6XwGirumGTloq9QD65KD5M8e1dsY6ep6jBmn7enL37Xn6IFgiI7KcQfHvABuWv6A2O62s7gtmEQ0HnchqXbknA5Xs5OxiQgTJU5bKNwetWsJOxCOxZKXZPSX5iN1DotXIg3mbyvwtBRR8y4qO");
ms.add(m);

为了更好地理解Java的工作原理,你可以参考以下书籍:

https://www.amazon.com/Head-First-Java-Kathy-Sierra/dp/0596009208

英文:

First of all your code it's not compiling (I bet that you already notice this).

Second, you must understand that when you want to add a message to your ArrayList, the message that you are trying to add must be compliant with the conditions that you are checking for in the if statement, in your case: the message should have a length of more than 280 characters and the messages ArrayList to not already contained the message that you are trying to add.

If you will replace this piece of code:

Message m = new Message(&quot;sender&quot;, &quot;message&quot;);
ms.add(asdf);

with this one:

Message m = new Message(&quot;sender&quot;, &quot;biUHobB2xW4Aed9jc5ZicRtgO1R2MUQGJeVHE3CBr0BxuSImM9E4ytkyeAKR22bEheJJIJ7PxzIsUjKYwsBKdYJeDA8VgKduTNJ0ofH66x3v20j3JnXZanWP0aRuxJmBatfmuEsBur8ald0eW6XwGirumGTloq9QD65KD5M8e1dsY6ep6jBmn7enL37Xn6IFgiI7KcQfHvABuWv6A2O62s7gtmEQ0HnchqXbknA5Xs5OxiQgTJU5bKNwetWsJOxCOxZKXZPSX5iN1DotXIg3mbyvwtBRR8y4qO
&quot;);
ms.add(m);

To have a better understanding of how Java works, you can check the following book:

https://www.amazon.com/Head-First-Java-Kathy-Sierra/dp/0596009208

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

发表评论

匿名网友

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

确定