ArrayList não popula

5 respostas
pyro

Pessoal, estou tentando popular um ArrayList da seguinte maneira:

ArrayList list = new ArrayList();
  Map author1 = new HashMap();
  author1.put("name", "A");
  author1.put("id", new Integer(1));
  list.add(author1);
  Map author2 = new HashMap();
  author2.put("name", "B");
  author2.put("id", new Integer(2));
  list.add(author2);
  Map author3 = new HashMap();
  author3.put("name", "C");
  author3.put("id", new Integer(3));
  list.add(author3);

for(int i = 0; i < list.size(); i++){
  System.out.println(list.get(i));
}

Porém no resultado do meu FOR ele fica em branco, somente exibe {}.
Alguém consegue identificar a cabeçada ou se exite uma forma mais fácil de popular esse arraylist?

5 Respostas

E

listarelatorio é a mesma coisa que list ou é uma outra coisa?

fbl.lucas
for(Object objeto:list){
  System.out.println(objeto);
}
ViniGodoy

Conforme o entaglement notou, você está preenchendo uma lista, mas tentando listar dados de outra...

Seu código também parece escrito em java 4. Em Java 7 ou superior seria assim:

List&lt;Map&gt;&lt;String, Object&gt;&gt; list = new ArrayList&lt;&gt;();
Map&lt;String, Object&gt; author1 = new HashMap&lt;&gt;();
author1.put(&quot;name&quot;, &quot;A&quot;);
author1.put(&quot;id&quot;, new Integer(1));
list.add(author1);

Map&lt;String, Object&gt; author2 = new HashMap&lt;&gt;();
author2.put(&quot;name&quot;, &quot;B&quot;);
author2.put(&quot;id&quot;, new Integer(2));
list.add(author2);

Map&lt;String, Object&gt; author3 = new HashMap&lt;&gt;();
author3.put(&quot;name&quot;, &quot;C&quot;);
author3.put(&quot;id&quot;, new Integer(3));
list.add(author3);

for(Map&lt;String, Object&gt; author : list){
  System.out.println(author);
}

Outra coisa... por que você está usando um Map, ao invés de criar uma classe Author? Ficaria ainda melhor, assim:

Em Author.java
public class Author {
   private int id;
   private String name;

   public void Author(int id, String name) {
      this.id = id;
      this.name = name;
   }

   public String getName() { return name; }
   public int getId() { return id; }

   @Override
   public String toString() { return "#" + id + " - " + name; }
}
No seu .java atual:
List&lt;Author&gt; list = new ArrayList&lt;&gt;();
list.add(new Author(1, &quot;A&quot;));
list.add(new Author(2, &quot;B&quot;));
list.add(new Author(3, &quot;C&quot;));

for(Author author : list){
  System.out.println(author);
}
pyro

Eu digitei errado, realmente é list, já corrigi no post.
Obrigado.

pyro
ViniGodoy:
Conforme o entaglement notou, você está preenchendo uma lista, mas tentando listar dados de outra...

Seu código também parece escrito em java 4. Em Java 7 ou superior seria assim:

List&lt;Map&gt;&lt;String, Object&gt;&gt; list = new ArrayList&lt;&gt;();
Map&lt;String, Object&gt; author1 = new HashMap&lt;&gt;();
author1.put(&quot;name&quot;, &quot;A&quot;);
author1.put(&quot;id&quot;, new Integer(1));
list.add(author1);

Map&lt;String, Object&gt; author2 = new HashMap&lt;&gt;();
author2.put(&quot;name&quot;, &quot;B&quot;);
author2.put(&quot;id&quot;, new Integer(2));
list.add(author2);

Map&lt;String, Object&gt; author3 = new HashMap&lt;&gt;();
author3.put(&quot;name&quot;, &quot;C&quot;);
author3.put(&quot;id&quot;, new Integer(3));
list.add(author3);

for(Map&lt;String, Object&gt; author : list){
  System.out.println(author);
}

Outra coisa... por que você está usando um Map, ao invés de criar uma classe Author? Ficaria ainda melhor, assim:

Em Author.java
public class Author {
   private int id;
   private String name;

   public void Author(int id, String name) {
      this.id = id;
      this.name = name;
   }

   public String getName() { return name; }
   public int getId() { return id; }

   @Override
   public String toString() { return "#" + id + " - " + name; }
}
No seu .java atual:
List&lt;Author&gt; list = new ArrayList&lt;&gt;();
list.add(new Author(1, &quot;A&quot;));
list.add(new Author(2, &quot;B&quot;));
list.add(new Author(3, &quot;C&quot;));

for(Author author : list){
  System.out.println(author);
}
Vini, achei sua sugestão de criar a classe Author excelente, porém, eu também irei precisar retornar o list em um JSP, do jeito que postei eu faria assim para iterar sobre o list utilizando JSTL:
&lt;c:forEach var="dado" items="${authors}"&gt;
  &lt;span&gt;<b>Pedido: </b>&lt;c:out value="${dado.id}"/&gt;&lt;/span&gt; |
  &lt;span&gt;<b>Transação: </b>&lt;c:out value="${dado.name}"/&gt;&lt;/span&gt; </br>
 &lt;/c:forEach&gt;
No servlet faria isso:
pageContext.setAttribute("authors", list);

Vc poderia ajudar em como iterar no retorno dentro do JSP no caso a sua solução com a classe Author?
Fica da mesma forma?

Grato pela ajuda!

Criado 15 de agosto de 2012
Ultima resposta 16 de ago. de 2012
Respostas 5
Participantes 4