Vetores no Java ME
De Aulas
Voltar para Programação para Dispositivos Móveis
Classe Principal
1import javax.microedition.midlet.*;
2import javax.microedition.lcdui.*;
3
4public class Main extends MIDlet {
5 private VectorCanvas vector;
6 private Display display;
7
8 public Main() {
9 display = Display.getDisplay(this);
10 vector = new VectorCanvas();
11 }
12
13 public void startApp() {
14 display.setCurrent(vector);
15 }
16
17 public void pauseApp() {
18 }
19
20 public void destroyApp(boolean unconditional) {
21 }
22}
Classe VectorCanvas
1import java.util.Enumeration;
2import java.util.Vector;
3import javax.microedition.lcdui.Canvas;
4import javax.microedition.lcdui.Graphics;
5
6class VectorCanvas extends Canvas {
7 private int position = 0;
8
9 public void paint(Graphics g) {
10 Vector vector = new Vector(3, 2);
11 println(g, "Tamanho do vetor: " + vector.size());
12 println(g, "Capacidade do vector: " + vector.capacity());
13
14 vector.addElement(new Integer(1));
15 vector.addElement(new Integer(2));
16 vector.addElement(new Integer(3));
17 vector.addElement(new Integer(4));
18 println(g, "Capacidade, depois de adicionados");
19 println(g, "--> elementos: " + vector.capacity());
20
21 vector.addElement(new Double(5));
22 println(g, "Capacidade atual: " + vector.capacity());
23
24 vector.addElement(new Double(6.1));
25 vector.addElement(new String("A7"));
26 println(g, "Capacidade atual: " + vector.capacity());
27
28 vector.addElement(new Float(9.23));
29 vector.addElement(new Integer(10));
30 println(g, "Capacidade atual: " + vector.capacity());
31
32 vector.addElement(new Integer(11));
33 vector.addElement(new Integer(12));
34 println(g, "Primeiro elemento: " + (Integer) vector.firstElement());
35 println(g, "Segundo elemento: " + (Integer) vector.lastElement());
36
37 if (vector.contains(new Integer(3))) {
38 println(g, "O vetor possui o numero 3.");
39 }
40
41 Enumeration vEnum = vector.elements();
42 String elementos = "";
43 while (vEnum.hasMoreElements()) {
44 elementos += " " + vEnum.nextElement();
45 }
46 println(g, "\nElementos no vetor:");
47 println(g, "--> " + elementos);
48 }
49
50 private void println(Graphics g, String texto) {
51 g.drawString(texto, 0, position, Graphics.LEFT|Graphics.TOP);
52 position += 10;
53 }
54}
Exercício
Crie uma pequena aplicação utilizando-se dos recursos de Vector e Canvas do Java ME. Procure pesquisar novos elementos, utilizar imagens, etc.