• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

Java question

ConstipatedVigilante

Diamond Member
I'm taking a computer science class at school. We're learning the basic stuff, but I can't remember how to convert an object (that was put into an ArrayList as an int) back to an int. It gives me the incompatible types error at the second for loop.

import java.util.*;

public class Permutator
{
public static int[] getPermutation(int[] a)
{
Random gen = new Random();
int temp;
int change;
int[] revert = new int[5];
ArrayList copy = new ArrayList();

for(int i = 0; i < 5; i++)
{
copy.add(a);
}

for(int i = 0; i < 5; i++)
{
temp = gen.nextInt(5);
revert = copy.get(change);
copy.remove(temp);
}
return revert;
}
public static void main()
{
int[] b = {1, 2, 3, 4, 5};
int[] c = getPermutation(b);

for(int i = 0; i < 5; i++)
{
System.out.println(c);
}
}
}

Edit: First time posting code here, and it comes out even more messed up in "attach code." Sorry.
 
if you are using java 1.5 It will do auto boxing and typecasting for you when you use generics.

// An Array List of only int/Integer objects.
ArrayList copy<Integer> = new ArrayList<Integer> ();
 
Back
Top