Member Avatar for sonicx2218
import java.util.*;
import java.io.IOException;
import java.util.Scanner;
import java.util.Random;


public class LinkedListProgram
{
    public static void main(String[] args) throws IOException
    {
        int i,number, ran;
        Scanner sc = new Scanner(System.in);
        LinkedList<Integer> list = new LinkedList<Integer>();
        ListIterator li;
        String line;
        Random random = new Random();
        int pick = random.nextInt(150);
        System.out.println("Enter # of nodes");
        number = sc.nextInt();
        ran = (1+ (int)(Math.random()*number));
        if (number > 0)
        {
            for (i = 0; i < number; i++)
            list.add(1+ (int)(Math.random()*150));
            Collections.sort(list);
            li = list.listIterator();
            String s = list.toString();
            System.out.println();
            System.out.println("  Index     Node");
            while (li.hasNext())
            {
            System.out.println("    " + li.nextIndex() + ".       " + li.next());


        }
        System.out.println("\n" + "    " + ran);




    }
        else
                System.out.println("\nnumber is less than 0\n");
    }
}

Right now my program lets a user type in a number and that many random numbers will be added to a linked list.

What I need help with is how to calculate the sum, mean, range and median of the numbers in the list. Would converting the list to an array be the most logical way? Is there a way I can do this via list iterator commands? I'm not very good with this.

The easiest way is just to use a "enhanced" for loop, as in

     for (Integer i : list) {
       sum += i; // etc
    }
Member Avatar for sonicx2218

Dude..I can't thank you enough! I'm so noobish at this, I didn't even know I could do that Thanks again!! It's working fine!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.