Member Avatar
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.

Recommended Answers

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

     for (Integer i : list) {
       sum += i; // etc
    }
Jump to Post

All 2 Replies

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

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

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 1.21 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.