Enviando um vetor para a tela(JTextField)

Olá pessoal!
Queria tirar uma dúvida, estou estudando estruturas de dados e estou recebendo erros quando mando um vetor para algum JTextField. :?

O código da função que criei é esse:

public int Fila() { //Estrutura da fila final int MAX = 100; int fila[] = new int[100]; //---------------- //declarando valor inicial do vetor for(int i = 0; i <= fila.length; i++) { t2.setText(Integer.toString(fila[i])); } return 0; }

quando clico em um botão, ele vai chamar essa função e passar os elementos do vetor para um JTextField(Na verdade quando clico no botão o JTextField aparece um 0) só que estou recebendo alguns erros e queria ajuda de vocês para compreensão dos mesmos:

Exception in thread “AWT-EventQueue-0” java.lang.ArrayIndexOutOfBoundsException: 100
at com.pibic.rafaelnunes.FormFila.Fila(FormFila.java:91)
at com.pibic.rafaelnunes.FormFila.actionPerformed(FormFila.java:58)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$000(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)

Grato.

O seu for esta indo até a posição 100 do array. A ultima posicao valida é 99.

muda para :

 for(int i = 0; i < fila.length; i++) {  
            t2.setText(Integer.toString(fila[i]));  
        }   

Bom ele executou sem erro, mas só aparece o primeiro elemento do array na tela que é o zero!
Como faz? :lol: AEUHEUHE.

Obrigado!

Quando você cria um Array, todas as posições desse Array são iniciadas com o valor padrão do tipo indicado. No seu caso, todas as posições possuem valor 0, uma vez que o tipo do Array é de ints e é o valor default dos tipos int é 0.

Antes de exibir o Array no textbox, voce precisa inicializá-lo, setando para cada posição o valor desejado.

Outra coisa…

Com esse código você sempre vai exibir o ultimo valor do array, pois você não esta concatenando os valores na textbox, esta alterando-o a cada iteraçao do for.

Dá uma olhada ai… qualquer duvida só perguntar…

Inicializando posições assim?

fila[0] = 1; fila[1] = 2; fila[2] = 3;

se for assim vou ter que setar todas as 100 posições né?!
E como concatenar isso em um text-box?

Isso… mas você pode fazer isso num loop… por exemplo:

Inicializar todas as posições com o mesmo valor do seu indice.

for (int i=0; i< fila.length; i++){
    fila[i] = i;
}

Depois você pode concatenar esses valores no jtextbox

for (int i=0; i< fila.length; i++){
    t2.setText(t2.getText() + " " +  Integer.toString(fila[i])); 
}

Lembrando que esse codigo serve apenas pra exemplificar o uso do array.

Entendi! :smiley:

Obrigado.