Ugo Posada
Posts: 37
Nickname: binaryx
Registered: Dec, 2002
|
|
Re: getting the first 10 values from a arraylist
|
Posted: Jan 16, 2003 8:43 PM
|
|
Here you go
import java.util.List;
import java.util.ArrayList;
import java.util.Collections;
public class arrayOrder {
private static int[] numbers = {1, 25, 87, 56, 43, 32, 45,
85, 76, 32, 99, 231, 4,77,12};
public arrayOrder() {}
public static int[] get10Ordered() {
List myList = new ArrayList();
for (int i=0; i < 15; i++) {
myList.add(new Integer(numbers[i]));
}
Collections.sort(myList);
Object[] myArray = myList.toArray();
int[] myIntArray = new int[10];
for (int i=0; i < 10; i++)
myIntArray[i] = ((Integer) myArray[i]).intValue();
return myIntArray;
}
public static int[] getFirst10() {
int[] response = new int[10];
for (int i=0; i < 10; i++)
response[i] = numbers[i];
return response;
}
public static void main(String[] args) {
for (int i=0; i < 15; i++)
System.out.println(numbers[i]);
System.out.println("----------------");
int[] temp = getFirst10();
for (int i=0; i < 10; i++)
System.out.println(temp[i]);
System.out.println("----------------");
temp = get10Ordered();
for (int i=0; i < 10; i++)
System.out.println(temp[i]);
}
}
|
|