The Artima Developer Community
Sponsored Link

Java Answers Forum
getting the first 10 values from a arraylist

2 replies on 1 page. Most recent reply: Jan 16, 2003 8:43 PM by Ugo Posada

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 2 replies on 1 page
ben

Posts: 57
Nickname: loftty
Registered: Oct, 2002

getting the first 10 values from a arraylist Posted: Jan 16, 2003 9:52 AM
Reply to this message Reply
Advertisement
Hello everyone
I hope someone can help me
I have a arraylist with 15 values in it.
[1,2,3,4,5,6,7,8,9,0,11,12,13,14,15]
how can only the first 10 values?
thanks
ben


Ugo Posada

Posts: 37
Nickname: binaryx
Registered: Dec, 2002

Re: getting the first 10 values from a arraylist Posted: Jan 16, 2003 11:22 AM
Reply to this message Reply
What do you mean, retrieve the first ten values as they are stored in the array or get the values from lower to greater in a sorted manner.

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
Reply to this message Reply
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]);         	
    }				
}
 

Flat View: This topic has 2 replies on 1 page
Topic: Why won't my array work? Previous Topic   Next Topic Topic: how to ping IPv6 with java

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use