Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: ArrayList of longs

  1. #1
    Member
    Join Date
    Sep 2011
    Posts
    63
    My Mood
    Confused
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default ArrayList of longs

    I'm following in the footsteps of the 100K people who have gone before me in trying to solve some of the project Euler problems. The challenge is to calculate the highest prime factor of a given number. My current solution works perfectly for integers... the problem is that project euler wants me to calculate a long, and I'm not sure I can use longs in ArrayLists.

    import java.util.*;
     
    public class PrimeFactors {
     
    	public static List<Integer> primeFactors(int numbers) {
    		int j = numbers; 
     
    		List<Integer> factors = new ArrayList<Integer>();
    		for (int i = 2; i <= j / i; i++) {
    			while (j % i == 0) {
    				factors.add(i);
    				j /= i;
    			}
    		}
    		if (j > 1) {
    			factors.add(j);
    		}
    		return factors;
    	}
     
    	public static void main(String[] args) {
    		int inputValue = 0;
     
    		if (args.length > 0) {
    			inputValue = Integer.parseInt(args[0]);
    		} else {
    			System.out.println("Please enter a value.");
    		}
    		for (Integer integer : primeFactors(inputValue)) {
    			System.out.println(integer);
    		}
    	}
    }

    Much appreciated if somebody could help point me in the right direction.


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: ArrayList of longs

    I have moved your thread to a more appropriate category.

    All primitives have wrapper classes...in the case of a long primitive you've got the Long class. You can use this class in place of the Integer class used in the code you posted, defining the List generic type to specify Long rather than Integer and changing all your primitives to long.

  3. The Following User Says Thank You to copeg For This Useful Post:

    tarkal (September 26th, 2011)

  4. #3
    Member
    Join Date
    Sep 2011
    Posts
    63
    My Mood
    Confused
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default Re: ArrayList of longs

    Thanks copeg, appolagies for the poor category placing. Damn those pesky capitals, foxed me again!!!

Similar Threads

  1. How to use an ArrayList and what is its advantage over array?
    By JavaPF in forum Java SE API Tutorials
    Replies: 4
    Last Post: December 21st, 2011, 04:44 AM
  2. Rounding long or floating numbers, or longs for that matter.
    By SPACE MONKEY in forum What's Wrong With My Code?
    Replies: 3
    Last Post: February 28th, 2011, 12:32 AM
  3. Ordering ArrayList by 3 conditions as you add to ArrayList
    By aussiemcgr in forum Collections and Generics
    Replies: 4
    Last Post: July 13th, 2010, 02:08 PM
  4. [SOLVED] Extracting an How to ArrayList from an ArrayList and convert to int??
    By igniteflow in forum Collections and Generics
    Replies: 2
    Last Post: August 16th, 2009, 01:11 PM
  5. How to use an ArrayList and what is its advantage over array?
    By JavaPF in forum Java Code Snippets and Tutorials
    Replies: 1
    Last Post: May 17th, 2009, 01:12 PM