So my java class is a joke, and we're expected to learn how to do our assignments via the web...I'm a hard worker, but I'm just not good at this.
Assignment pic
There's the assignment image. I think i got how to do the first part with the adding random numbers to the list and sorting...but I can't find other resources online that help me understand the rest. Here's my code right now

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);
        ListIterator listIterator = list.listIterator();
        System.out.println("Enter # of nodes");
        number = sc.nextInt();
        if (number > 0)
        {
            for (i = 0; i < number; i++)
            list.add(1+ (int)(Math.random()*150));
            Collections.sort(list);
            System.out.print(list + "\n");
            System.out.println(listIterator.nextIndex());

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

Thanks for any help solving this. I'm not familiar with any of this stuff, and everything I do know is based off of what I've found via google searches. I'm nowhere near as profficient at this as you may think.

Out of everything I'm trying to do, the thing that gives me the most trouble is making my linked list include index numbers to the left of the actual numbers. Right now my list look like [45, 140, 78], but I want my numbers to have the index numbers to their left and look like:

0 45
1 140
2 78
(I'm not positive if the first index number is 0 or 1)

That print out looks like what comes from the list class's toString() method. You will need to do your own formatting for the output you are looking for.

Ok that does help a bit, I never thought of trying that. Just a question. I placed the list into a string. I've tried to post the code I used but I get some weird error when I try to post here so i can't give you it now.
It prints without the brackets now, but I was wondering if you knew how to make a string print the numbers out one per line instead of them all on one line?

String s = list.toString();
System.out.println(s.substring(1, s.length()-1));

here's the code, I think it'll let me post this time

one per line

Use the println method to print what you want on a line by itself.

Edited 4 Years Ago by NormR1

This question has already been answered. Start a new discussion instead.