Placing two attributes in a cell

Asked

Viewed 67 times

1

Have some structure, List, LinkedList, etc., that allows me to put two attributes in the cell? For example, an integer and a string in each cell.

  • Could you explain what the use would be? I found your question a little vague, I’m not sure what your question is.

2 answers

2

You’d have to do something like this:

class MinhaEstutura {
    public int MeuInteiro;
    public String MinhaString;
}

The structure, in this case, would look like this:

ArrayList<MinhaEstrutura> lista = new ArrayList<MinhaEstrutura>();
  • 1

    Just one detail, but to follow the Java pattern, I suggest starting the variable name with lowercase: meuInteiro, minhaString.

0

Example

//Criação e Adição
Map<Integer, String> dic = new HashMap<>();
dic.put(1, "Nome 1");
dic.put(2, "Nome 2");
dic.put(3, "Nome 3");
dic.put(4, "Nome 4");

//Leitura de cada item adicionado
Iterator<Map.Entry<Integer,String>> itr =  dic.entrySet().iterator();
while (itr.hasNext()){
    Entry<Integer,String> item = itr.next();
    System.out.println(item.getKey().toString() + " " + item.getValue());
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.