1

This is my second program in Java, and its the first time I'm using an arrayList. I searched about how to convert it, and used the methods i found, but I get an error...

package eliminarepetidos;
import java.util.ArrayList;
import java.util.Random;
public class Eliminarepetidos {

public static ArrayList<Integer> eliminaRepetidos (int[] vet){
    ArrayList<Integer> retorna = new ArrayList<>();
    for(int i = 0; i<vet.length; i++){
        for (int j = i + 1; j < vet.length; j++)
            if ((vet[i] == vet[j])&&(vet[i]!=0)) vet[j]=0;                      
        if(vet[i]!=0) retorna.add(vet[i]); }
    return retorna;
}

public static void imprime (int[] vet, int numElem){
    System.out.print("Vetor resultante:");
    for (int i = 0;i<numElem;i++)    
        System.out.print(" " +vet[i]);       
}

public static void main(String[] args) {

    int[] t;
    t = new int[10];
    Random generator = new Random();
    for(int i = 0; i<10; i++)
        t[i] = generator.nextInt(12) +9;
    ArrayList<Integer> temporario = new ArrayList<>();
    temporario = eliminaRepetidos(t);
    int [] vetfinal = temporario.toArray(new int[temporario.size()]); //line with error
    imprime(vetfinal,vetfinal.length);
}
}

How should I be using the command to make it work properly? Thanks!

SOfanatic
  • 5,523
  • 5
  • 36
  • 57
  • what error are you getting? – Chubby Boy Jun 08 '13 at 14:05
  • 1
    The problem is you have an arraylist of Integers and try to make an array of ints (primitives) of it. You can find similar questions on SO for instance here http://stackoverflow.com/questions/718554/how-to-convert-an-arraylist-containing-integers-to-primitive-int-array – Mateusz Dymczyk Jun 08 '13 at 14:08
  • Thanks, @MateuszDymczyk ! I solved my problem with your link. I don't know how to vote your comment as useful, otherwise I would already have done it! – Lucas Tiago Jun 08 '13 at 14:48
  • @LucasTiago: I did not answer your question because I flagged it as a duplicate, hopefully someone will vote to close it. No offence but I like my SO without redundancies :-) – Mateusz Dymczyk Jun 08 '13 at 15:03
  • @MateuszDymczyk no problems, started using SO today! Sry for any inconvenience =X – Lucas Tiago Jun 08 '13 at 15:28

1 Answers1

0

Any ArrayList can be converted to a simple array using toArray(<Type> t) (ArrayList<Thing> list -> Thing[] arr = list.toArray(new Thing[0])), but that's likely not your real question.

Your real question, based on the code you've shown, is far more likely: "how do I iterate over an arraylist?", to which the answer is: "the same as for arrays or any other collection, use the for loop in its foreach pattern":

int[] numbers = {...}
ArrayList<Thing> things = new ArrayList<Thing>();
thing.add(new Thing(...));
...

for(int i: numbers) {
  System.out.println(i); // will print each element in the array
}

for(Thing t: things) {
  System.out.println(t); // will print each element in the arraylist
}

This use of the for loop has been part of plain Java since v1.5 =)

Mike 'Pomax' Kamermans
  • 49,297
  • 16
  • 112
  • 153