Chamar Arraylist de outro classe

Queria puxar um Arraylist para a outra classe, como faço?

Deve ter como fazer algo assim:

List<SeuObjet> seuArrayList = outraClasse.getSuaLista();

Foi mal, Não entendi exatamente a dificuldade.

1 curtida

É por que esse Arraylist está dentro do método main.

Dessa forma não é possivel acessar esse arraylist de outro lugar. Se quiser acessar essa lista, você deve colocá-la como atributo de classe e disponibilizar um método para acessar esse atributo.

puts

Como está esse seu método main?

1 curtida

A arraylist no caso é “resultados”

import com.gargoylesoftware.htmlunit.BrowserVersion;
import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.html.HtmlElement;
import com.gargoylesoftware.htmlunit.html.HtmlPage;
import com.gargoylesoftware.htmlunit.html.HtmlSpan;
import com.gargoylesoftware.htmlunit.html.HtmlTable; 
import com.gargoylesoftware.htmlunit.html.HtmlTableCell;
import java.io.IOException;
import java.net.CookieHandler; 
import java.net.CookieManager;
import java.net.CookiePolicy; 
import java.util.ArrayList; 
import java.util.List; 

public class Loto {

	public static void main(String[] args) throws IOException {
		java.util.logging.Logger.getLogger("com.gargoylesoftware.htmlunit").setLevel(java.util.logging.Level.OFF);
		java.util.logging.Logger.getLogger("org.apache.http").setLevel(java.util.logging.Level.OFF); 
		
		CookieHandler.setDefault(new CookieManager(null, CookiePolicy.ACCEPT_ALL)); 
		String URL ="http://www.loterias.caixa.gov.br/wps/portal/loterias/landing/lotofacil";
		
		WebClient webClient = new WebClient(BrowserVersion.getDefault());
		HtmlPage page = webClient.getPage(URL);
		webClient.getOptions().setJavaScriptEnabled(true);
		webClient.waitForBackgroundJavaScript(9000);
		System.out.println(page.getTitleText());
		System.out.println(page.getUrl().toString()); 
		
		List<HtmlElement> spans = page.getBody().getElementsByAttribute("span", "class", "ng-binding"); 
		HtmlSpan concurso = (HtmlSpan) spans.get(0); 
		
		String ccs = concurso.getTextContent().replaceAll("\\s+", " ").trim(); 
		System.out.println(ccs); 
		
		List<Long> resultados = new ArrayList(); List<HtmlElement> lista = page.getBody().getElementsByAttribute("table", "class", "simple-table lotofacil"); 
		HtmlTable tabela = (HtmlTable) lista.get(0); 
		
		List<HtmlTableCell> cells = tabela.getBodies().get(0).getElementsByAttribute("td", "ng-repeat", "dezena in resultadoLinha"); 
		cells.forEach(cell -> resultados.add(Long.parseLong(cell.getTextContent())));
		
		System.out.println(resultados);
	}
}

e como eu faria isso?

Qual classe precisar acessar esse resultado? Como vc está chamando essa outra classe?

1 curtida

Mude sua classe Loto para ficar assim:

public class Loto {

	public List<Long> recuperarResultados() throws IOException {
		java.util.logging.Logger.getLogger("com.gargoylesoftware.htmlunit").setLevel(java.util.logging.Level.OFF);
		java.util.logging.Logger.getLogger("org.apache.http").setLevel(java.util.logging.Level.OFF); 
		
		CookieHandler.setDefault(new CookieManager(null, CookiePolicy.ACCEPT_ALL)); 
		String URL ="http://www.loterias.caixa.gov.br/wps/portal/loterias/landing/lotofacil";
		
		WebClient webClient = new WebClient(BrowserVersion.getDefault());
		HtmlPage page = webClient.getPage(URL);
		webClient.getOptions().setJavaScriptEnabled(true);
		webClient.waitForBackgroundJavaScript(9000);
		System.out.println(page.getTitleText());
		System.out.println(page.getUrl().toString()); 
		
		List<HtmlElement> spans = page.getBody().getElementsByAttribute("span", "class", "ng-binding"); 
		HtmlSpan concurso = (HtmlSpan) spans.get(0); 
		
		String ccs = concurso.getTextContent().replaceAll("\\s+", " ").trim(); 
		System.out.println(ccs); 
		
		List<Long> resultados = new ArrayList(); List<HtmlElement> lista = page.getBody().getElementsByAttribute("table", "class", "simple-table lotofacil"); 
		HtmlTable tabela = (HtmlTable) lista.get(0); 
		
		List<HtmlTableCell> cells = tabela.getBodies().get(0).getElementsByAttribute("td", "ng-repeat", "dezena in resultadoLinha"); 
		cells.forEach(cell -> resultados.add(Long.parseLong(cell.getTextContent())));
		
		return resultados;
	}
}

E faça a chamada dessa forma:

private List<Long> resultadoLoto;

// mais código (que não quis colocar nesse exemplo)

jButton1.addActionListener(new ActionListener() {
	@Override
	public void actionPerformed(ActionEvent ae) {
		Loto loto = new Loto();
		
		try {
			this.resultadoLoto = loto.recuperarResultados();
		} catch (IOException ex) {
			Logger.getLogger(Ltous.class.getName()).log(Level.SEVERE, null, ex);
		}
	}
});

Deu um erro aqui, "create field to “resultadoLoto”

Onde vc colocou a propriedade “resultadoLoto” ? Posta a classe completa.

E esse erro deu em qual linha?

	nessa - >	this.resultadoLoto = loto.recuperarResultados();

Essa linha de código está na classe Ltous?

sim,
try {
this.resultadoLoto = loto.recuperarResultados();
} catch (IOException ex) {

Crie esse método na classe:

public static void setResultadoLoto(List<Long> resultadoLoto) {
	this.resultadoLoto = resultadoLoto;
}

E mude de:

this.resultadoLoto = loto.recuperarResultados();

para:

setResultadoLoto(loto.recuperarResultados());

no Ltous né?

Sim. Pois é nessa classe que existe o atributo resultadoLoto.

Deu um novo erro agora kkk,

non-static variable this cannot be referenced from a static context
this.resultadoLoto = resultadoLoto;

public static void setResultadoLoto(List resultadoLoto) {
nessa linha -> this.resultadoLoto = resultadoLoto;
}