ArrayList <String> fil = new ArrayList<String>();
ArrayList <String> lif = new ArrayList<String>();
int x=0;
long tim = System.currentTimeMillis();
Random random = new Random(tim);
for(int i=0;i<fil.size();i++)
{
x =random.nextInt(fil.size());
for(int y=0;y<lif.size();y++)
{
if(fil.get(x).equals(lif.get(y)))
{
i--;
continue;
}
}
System.out.println("Set the value of x"+x);
lif.add(i, fil.get(x));//array index out of bound exception
}
I’m trying to copy the contents of ArrayList fil to lif in a different order, but I get an ArrayIndexOutOfBoundException in the commented line. I’m using a nested for-loop to check if the elements of new ArrayList are unique.
Why don’t you try Collections.copy and Collections.shuffle methods.