Ingest data to table jsp

0

Good afternoon.

I am currently doing a program, which has a LinkedList, at the moment of wanting to apply the Method lista.contains ("Mario") returns false, it should be noted that the list is filled according to a class of the following shape ;

lista.add(new VideojuegoBean("Fifa 2017", "Deporte",120,"5","A"));
        lista.add(new VideojuegoBean("Halo", "Accion", 295,"2","B"));
        lista.add(new VideojuegoBean("PES 2014", "Deportes", 309,"4","C"));
        lista.add(new VideojuegoBean("Gears ", "Accion", 409,"3","B"));
        lista.add(new VideojuegoBean("Mario", "Fantasia", 509,"1","A"));
        lista.add(new VideojuegoBean("Mario Word", "Fantasia", 609,"4","C"));

For which I understand you should return true since it is in the list if it contains it, the other data in parentheses are part of the attributes of the class VideogameBean, It should be noted that if I apply the function

form.getLista().indexOf("Mario"); 

I should return the index but it returns -1 and I think this happens because it is not found.

Try to try several forms but none can determine if the list contains any element.

Hopefully you can guide me to know if I'm using a method wrong.

The list view is displayed like this;

VideojuegoBean{titulo=Fifa 2017, genero=Deporte, cantidadJugadores=5, clasificacion=A, codigo=120}
VideojuegoBean{titulo=Halo, genero=Accion, cantidadJugadores=2, clasificacion=B, codigo=295}
VideojuegoBean{titulo=PES 2014, genero=Deportes, cantidadJugadores=4, clasificacion=C, codigo=309}
VideojuegoBean{titulo=Gears , genero=Accion, cantidadJugadores=3, clasificacion=B, codigo=409}
VideojuegoBean{titulo=Mario, genero=Fantasia, cantidadJugadores=1, clasificacion=A, codigo=509}
VideojuegoBean{titulo=Mario Word, genero=Fantasia, cantidadJugadores=4, clasificacion=C, codigo=609
    
asked by Arturo Vera 19.03.2017 в 23:03
source

1 answer

0

First, your problem has nothing to do with JSPs, databases, arrays or webservices. If you titrate and catalog your questions properly, it will be easier for them to find an answer. And I recommend you start programming with Java SE (without adding the complexities of JSPs or BD) before trying to do more complicated things.

The problem is that what you are inserting in the list instances of VideojuegoBean and then looking for an instance of String . When you do a search, the last step 1 will be to compare both objects by calling the equals of one of them to see if it returns true .

There are two possibilities:

  • that the instruction is "Mario".equals(new VideojuegoBean("Mario", "xxxx")) , which will always return false , and that can not be changed (it would require changing the definition of equals of String , which is not only impossible but very dangerous.

  • that the instruction is ((new VideojuegoBean("Mario", "xxxx")).equals("Mario") . Here, in the implementation of VideojuegoBean , you could make the implementation of equals , if compared to a String , equals the comparison with the title. But it's HIGHLY UNACCEPTABLE , since then you have that "Mario".equals(unObjeto) is not equal to unObjeto.equals("Mario") , which goes against the specifications of Mario.

  • Also, since that statement is executed as part of the API, you will never know which of the two will be executed.

Solution:

Well, to begin with, since you're starting, the old method is to iterate through the list (use a Iterator better); for each video game you get its title and then you compare with "Mario".

 Iterator<VideojuegoBean> it = lista.iterator();
 VideojuegoBean encontrado = null;
 while ((it.hasNext() && (encontrado == null)) {
   VideojuegoBean vb = it.next();
   if ("Mario".equals(vb.getTitulo()) {
     encontrado = vb;
   }
 }

If you have Java 8, once you have more ease with Java you can use streams , which allow you to do the same with something less than code.

1 In which subclasses of Collection you will also have to make the hashCode consistent.

2 Capital letters, bold, italic ... would you leave me something to show what a bad idea it would be?

    
answered by 20.03.2017 / 00:27
source