Boa tarde caros amigos do guj.
Meu problema eh o seguinte:
Na linha 128 do codigo a referencia que eu tento fazer
l=lista;
não funciona e o argumento(uma lista de listas que devem estar preenchidas ) passado na linha 80, chamando
o método buscaBinária não será executado corretamente.
Queria uma sugestão para corrigir esse problema da referência ou senão como escrever o tipo de
retorno do método preencherLista sendo um tipo genérico.
Um abraço e obrigado pela atenção de todos!
package br.com.teste7;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
/**
*
* @author tarcisio.ferreira
*
*/
@SuppressWarnings("serial")
class SimpleIntegerList extends ArrayList<Integer> {
private List<Integer> elementos;
public SimpleIntegerList() {
elementos = new ArrayList<Integer>();
}
public SimpleIntegerList(List<Integer> a) {
elementos = a;
}
public List<Integer> getElementos() {
return elementos;
}
public void setElementos(List<Integer> elementos) {
this.elementos = elementos;
}
public void print() {
for(Integer e: elementos) {
System.out.println(e.toString());
}
}
}
@SuppressWarnings("serial")
class SimpleDoubleList extends ArrayList<Double> {
private List<Double> elementos;
public SimpleDoubleList() {
elementos = new ArrayList<Double>();
}
public SimpleDoubleList(List<Double> a) {
elementos = a;
}
public List<Double> getElementos() {
return elementos;
}
public void setElementos(List<Double> elementos) {
this.elementos = elementos;
}
public void print() {
for(Double e: elementos) {
System.out.println(e.toString());
}
}
}
public class Test {
public static void main(String... args) throws Throwable, IOException {
Test t = new Test();
SimpleIntegerList i = new SimpleIntegerList();
SimpleDoubleList d = new SimpleDoubleList();
List<ArrayList> l = new ArrayList<ArrayList>();
l.add(i);
l.add(d);
t.preencheListas(l);
buscaBinaria(l, new Integer("90"));
}
public void preencheListas( List<? extends ArrayList> d) {
for (List l:d) {
if (l instanceof SimpleIntegerList) {
Integer ant, prox;
int MAX = 100;
List<Integer> lista = new ArrayList<Integer>();
ant = prox = new Integer("-1");
for(int i=0; i < MAX;i++) {
if ( i==0 ) {
ant = prox = new Integer((int) (1 + (Math.random() * 100)));
lista.add(prox);
}else {
if ( ant.intValue() == prox.intValue() ) {
Integer aux = null;
do {
aux = new Integer((int) (1 + (Math.random() * 100)));
prox = aux;
} while(ant.intValue() == prox.intValue());
if (!lista.contains(prox))
lista.add(prox);
ant = prox;
prox = new Integer((int) (1 + (Math.random() * 100)));
}else {
ant = prox;
if (!lista.contains(prox))
lista.add(prox);
prox = new Integer((int) (1 + (Math.random() * 100)));
}
}
}
l=lista;
}else {
System.out.println("Not implemented yet.");
}
}
}
@SuppressWarnings("unchecked")
public static void buscaBinaria(List<? extends ArrayList> d, Object key) {
for (List l:d) {
if (l instanceof SimpleIntegerList) {
int x,e,g;
e = 1;
g = d.size();
List<Integer> sIntList = new SimpleIntegerList();
sIntList = l;
do {
x=(e+g)/2;
if((Integer)key < sIntList.get(x))
e=x-1;
else
e=x+1;
}while((Integer)key != sIntList.get(x) && e<g);
if((Integer)key == sIntList.get(x))
System.out.println("Encontrei "+ (Integer)key + " .");
else
System.out.println("O numero digitado nao estah na sequencia gerada.");
}
}
}
public void buscaLinear(List<? extends ArrayList> d, Object key) {
}
}