package Exercise06_LongestInCollection;
import java.util.ArrayList;
public class SimpleCollection {
private String name;
private ArrayList<String> elements;
public SimpleCollection(String name) {
this.name = name;
this.elements = new ArrayList<>();
}
public void add(String element) {
this.elements.add(element);
}
public ArrayList<String> getElements() {
return this.elements;
}
public String longest() {
if(this.elements.isEmpty()) {
return null;
}
String returnObject = this.name;
for (String name : this.elements) {
if (returnObject.length() < name.length()) {
returnObject = name;
}
}
return returnObject;
}
}
``` and im talking about the longest method it kinda works but it responds with the simplecollection name
#I need to exclude the arraylists name from this:
1 messages · Page 1 of 1 (latest)
<@&987246399047479336> please have a look, thanks.
While you are waiting for getting help, here are some tips to improve your experience:
If nobody is calling back, that usually means that your question was not well asked and hence nobody feels confident enough answering. Try to use your time to elaborate, provide details, context, more code, examples and maybe some screenshots. With enough info, someone knows the answer for sure.
Don't forget to close your thread using the command </help-thread close:1027500463647621170> when your question has been answered, thanks.
Here is an AI assisted attempt to answer your question 🤖. Maybe it helps! In any case, a human is on the way 👍. To continue talking to the AI, you can use </chatgpt:1108714622413963314>.
tion {
private ArrayList<String> names;
public SimpleCollection() {
names = new ArrayList<>();
}
public void addName(String name) {
names.add(name);
}
public void removeName(String name) {
names.remove(name);
}
public boolean containsName(String name) {
return names.contains(name);
}
public int getSize() {
return names.size();
}
}
To exclude the arraylist's name from the code, you can change the variable declaration and method signatures to use a more generic type, such as `List<String>` instead of `ArrayList<String>`. Here's the modified code:
```java
package Exercise06_LongestInCollection;
import java.util.List;
import java.util.ArrayList;
public class SimpleCollection {
private List<String> collection;
public SimpleCollection() {
collection = new ArrayList<>();
}
public void addElement(String element) {
collection.add(element);
}
public void removeElement(String element) {
collection.remove(element);
}
public boolean containsElement(String element) {
return collection.contains(element);
}
public int getSize() {
return collection.size();
}
}
In this modified code, the arraylist's name "names" has been replaced with a more generic name "collection". The variable declaration and method signatures now use List<String> instead of ArrayList<String>.
package Exercise06_LongestInCollection;
public class Main {
public static void main(String[] args) {
// Try out your class here
SimpleCollection c = new SimpleCollection("characters");
System.out.println("Pisin: " + c.longest());
c.add("magneto");
c.add("mystique");
c.add("phoenix");
System.out.println("Longest: " + c.longest());
}
}
``` this is the main code
ur titles lead to people skipping ur threads btw
better 🙂
well you didnt skip xD
is the AI's way the only way?
idk it sounded right when I wrote it now it makes no sense lmao and what you mean with the longest() method before the for loop?
oh the returnObject saves the longest name
I cant overwrite this.name or can I?
the name with the most characters
well you could say I want to return the longest name
(also PING ME PLS thanks)
(dont take that as an insult)
so I just have to switch the < to > right?
oh I get what you want
hold on
public String longest() {
if(this.elements.isEmpty()) {
return null;
}
for (String name : this.elements) {
if (this.name.length() > name.length()) {
String returnObject = name;
}
}
return ;
}
``` did you want that? but now I cant return returnObject
Why are you returning null if elements is empty ? name doesn't count?
the exercise wants that
public String longest() {
if(this.elements.isEmpty()) {
return null;
}
String returnObject;
for (String name : this.elements) {
if (this.name.length() > name.length()) {
returnObject = name;
}
}
return ;
}
``` so you mean like this?
ok now its an error when I run the code because it says returnObject is not initialized
so I guess you dont want me to return it but to compare it to something
so do I give returnObject the first index of elements?
give me a hint
I have no idea
I have lunch now so I will think about it during lunch
well then I was right with taking an Index
yeah thats what I meant
public String longest() {
if(this.elements.isEmpty()) {
return null;
}
String returnObject = this.elements.get(0);
for (String name : this.elements) {
if (returnObject.length() < name.length()) {
returnObject = name;
}
}
return returnObject;
}
}
Detected code, here are some useful tools:
it takes the element thats at index 0
if the list isnt empty there will be 100% something there
yeah thats what I meant with this
maybe you didnt exactly know what I meant
why u are making ur code complicated
in the main method , u will take one name with the help of constructor , and the put it in that name in that variable , and then u will put that name in the list , and then u will call longest() , method to exrtact what ?
i wan to know what exactly u want to do in this ciode ?\