marcio duran wrote:
I'm trying to run this code, but the routine that I put to you to enter five elements in the ArrayList, and simply does not enter the 6th element, because it does not work, below is a Routine
[HENRY: Formatting modified]
When you start adding elements, it must hold in this case it will add the list until you reach the maximum of 5 elements, then how do you code this condition that could illustrate for this condition?Henry Wong wrote:
marcio duran wrote:
I'm trying to run this code, but the routine that I put to you to enter five elements in the ArrayList, and simply does not enter the 6th element, because it does not work, below is a Routine
[HENRY: Formatting modified]
Agreed. A sixth element is never added. From your code, the code that conditional add elements, is in a condition that is only triggered if there are less than 5 elements. Since the code can never be triggered when there are five or more elements, obviously, it can never be six elements.
Henry
/* public boolean armazena(String newName){
for (int i=0;LISTA_DE_NOMES.size()<=5; i++){
if(LISTA_DE_NOMES.get(i).equals(newName))
return true;
}
LISTA_DE_NOMES.add(newName);
return false;
}*/
Campbell Ritchie wrote:The way you are handling that code, you are not using the List as a List. You are actually using it as a Set, because you are not permitting duplicates. Do you require a Set which does not support duplicates, in combination with recording the order of insertion. If so, go through the Java Tutorials. You will find all sorts of implementations, including one which combines a linked list and a Set. That might provide what you want all in one class.
Campbell Ritchie wrote:If you are only adding new elements to the List, you do not have a List, but an ordered Set. I suggest you consider one of the implementations of java.util.Set which records insertion order instead of a List.
There aren’t very many to choose from, unless you write your own
marcio duran wrote:Será que seria perto disso , bom eu estou pesquisando !!! Set<String> setObject = new HashSet<String>(listObject);
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
Winston Gutkowski wrote:
marcio duran wrote:Será que seria perto disso , bom eu estou pesquisando !!! Set<String> setObject = new HashSet<String>(listObject);
Sounds like a good start to me, but there's no experience like doing.
Try it out, and see if you run into problems.
Winston
marcio duran wrote:"Friends, I'm seeing a lot of codes and some examples in some implementations it seems to me that imagination comes to amazing algorithms format in Java language, but it is necessary to share global experiences and this is called science"
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
Campbell Ritchie wrote:We speak English on this website!
As Winston said, by doing.
Winston Gutkowski wrote:
marcio duran wrote:"Friends, I'm seeing a lot of codes and some examples in some implementations it seems to me that imagination comes to amazing algorithms format in Java language, but it is necessary to share global experiences and this is called science"
Give a man a fish and you feed him for a day. Teach a man to fish and you feed him for a lifetime.
Winston
marcio duran wrote:
Campbell Ritchie wrote:We speak English on this website!
As Winston said, by doing.
We speak Java on this website !
Henry Wong wrote:
marcio duran wrote:
Campbell Ritchie wrote:We speak English on this website!
As Winston said, by doing.
We speak Java on this website !
Agreed. However, JavaRanch is a moderated site, and unfortunately, we do not have enough moderators for all the forums (if we have to deal with every language). Hence, we do require that all non-code be posted in English.
Thank you for your understanding,
Henry
Campbell Ritchie wrote:I don’t understand the last post.
Marcio Duran wrote:"Writing in English often do not help much when I'm thinking in Portuguese, sorry"
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
Winston Gutkowski wrote:
Marcio Duran wrote:"Writing in English often do not help much when I'm thinking in Portuguese, sorry"
Then I fear you'll have to find a Portugese site.
Also: please don't write in all bold text. It comes across as "shouting".
Winston
Marcio Duran wrote:I think it should come, what I'm wondering
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
Winston Gutkowski wrote:
Marcio Duran wrote:I think it should come, what I'm wondering
What are you wondering? We still don't know yet.
However, a few points about your code:
1. Don't use ALL_CAPS for field names: they should start with a lower-case letter. See here or here for a list of the main naming conventions.
2. Don't simply throw Exception. At the very least you should throw a RuntimeException, because that's what it is - you also don't have to declare it in the method signature - but better still is to throw an Exception of your own that describes the error (RoomSizeExceededException?).
3. Your code is brittle because you've hard-wired the maximum room size. What about supplying it at construction time?:then you can set up a room with:
new Sala(5);
and check whether you've exceeded its limit with:
if(listaDeNomes.size() >= listaDeNomes.getMaxSize()) {
throw new RoomSizeExceededException("Esta sala já esta cheia");
} ...
Better still: put ALL that logic in Sala itself; perhaps a novoUsario(String) method.
HIH
Winston
Marcio Duran wrote:Thank You !!!
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
Winston Gutkowski wrote:
Marcio Duran wrote:Thank You !!!
Don't thank me yet:
1. java.util.Set is an interface, NOT a class; so you can implement it any way you like.
2. Furthermore: most Java classes that do implement it (eg, java.util.HashSet) are NOT final; therefore you can subclass them.
3. All that "is it OK to add this user" logic (including your armazena() and canAddNewUser() methods) can therefore (and should) be put inside your Sala class.
Given that, show us what your Sala class should look like.
Winston
Marcio Duran wrote:I want to ask It's valid!
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
Winston Gutkowski wrote:
Marcio Duran wrote:I want to ask It's valid!
Fraid not:
What if nomeCliente.size() is > 5? What if adding all the names in nomeCliente would make LISTA_DE_NOMES.size() > 5?
Keep plugging away. You'll get there.
Winston
Campbell Ritchie wrote:Finish the validation here before you connect to a socket.
Marcio Duran wrote:
Campbell Ritchie wrote:Finish the validation here before you connect to a socket.
I'm thinking the designer code, programming was in second place