KK i gots a java question

Status
Not open for further replies.

wafflehammer

Fully Optimized
Messages
2,502
Location
Logan, WV
I just got out of my final exam for comp sci 1 and the last part of the lab we had was to make it so the arraylist was maintained in numerical order.

How do you do this? I couldn't find anything on the java API. The only thing I could find that made sense was the "sort" method but I think it was for arrays and not arraylists :\ So needless to say that portion of the lab was not there when I turned it in :( So I'd like to know this for future reference :D

Here's the rest of the code if that matters

Code:
import java.util.ArrayList;
import java.util.Scanner;
public class ArrayFinal
{
    public static void main(String[] args)
    {
        Scanner myScanner = new Scanner(System.in);
        boolean done = false;
        String answer;
        String response;
        ArrayList<Double> myList;
        myList = new ArrayList<Double>();
        
        do
        {
            System.out.println("1: Add new number");
            System.out.println("2: Delete number");
            System.out.println("3: Print all the numbers");
            System.out.println("4: Find the sum");
            System.out.println("5: Find the average");
            System.out.println("6: Print list size");
            System.out.println("7: QUIT");
            System.out.println();
            answer = myScanner.nextLine();
            int userChoice = Integer.parseInt(answer);
            
            if(userChoice==1)
            {
                System.out.println();
                Scanner aScanner = new Scanner(System.in);
                
                System.out.println("Enter a new number to add:");
                
                response = myScanner.nextLine();
                double entry = Double.parseDouble(response);
                
                myList.add(entry);
                System.out.println(entry +" has been added to the list.");
                System.out.println();
            }
            
            else if(userChoice==2)
            {
                System.out.println();
                Scanner aScanner = new Scanner(System.in);
                
                System.out.println("Enter a number to remove:");
                
                response = myScanner.nextLine();
                double entry = Double.parseDouble(response);
                
                if(myList.contains(entry))
                {
                    myList.remove(entry);
                    System.out.println(entry +" has been removed from the list.");
                }
                else
                    System.out.println("The number "+entry+" is not in the list.");
                System.out.println();
            }
            
            else if(userChoice==3)
            {
                System.out.println();
                Scanner aScanner = new Scanner(System.in);
                
                System.out.println("Printing the entries in the list :");
                
                for(int i = 0; i < myList.size(); i++)
                System.out.println(myList.get(i));
                System.out.println();
            }
            
            else if(userChoice==4)
            {
                System.out.println();
                
                double sum = 0;
                for( int a = 0; a < myList.size(); a++)
                sum = sum + myList.get(a);
                
                System.out.println("The sum of the entries is " + sum);
                System.out.println();
            }
            
            else if(userChoice==5)
            {
                System.out.println();
                double sum = 0;
                for( int b = 0; b < myList.size(); b++)
                sum = sum + myList.get(b);
                
                double avg = 0;
                avg = sum/myList.size();
                
                System.out.println("The average of the entries is " + avg);
                System.out.println();
            }
            
            else if(userChoice==6)
            {
                System.out.println();
                Scanner aScanner = new Scanner(System.in);
                
                System.out.println("There are " +myList.size()+ " entries.");
                System.out.println();
            }
            
            else if(userChoice==7)
            {
                System.out.println();
                done = true;
                System.out.println("Good Bye!");
            }
            
            else
            {
                System.out.println();
                System.out.println("Wrong Choice!");
                System.out.println();
            }
            
            System.out.println();
            
        }while (done==false);
            System.exit(0);
    }
}


I'm sure this is just simple easy stuff :p But it's a start I guess lol
 
well arraylist was like the highest thing we learned..so I would think I would have to keep it within my level if you know what i mean
 
Did it have to be an ArrayList? TreeSet does exactly this.

He won't learn TreeSet or generic stuff for another class or two I'd imagine.

You should be able to just make a loop using compareTo (assuming the objects in the array list are comparable) to sort it the way you needed. An iterator would help too. Maybe 8 lines of code?

Code:
public void sortArray(ArrayList a){
int size = a.size();
Object min = a.get(1); //(object is for whatever you have in array list)
for(int i = 1; i < size; i++){
     if(min.compareTo(a.get(i+1)) < 0){
          ; //do nothing
     }
     else{
          min = a.get(i+1);
          a.set(i+1, a.get(i));
          a.set(i, min);
     }
}
}

Something like that. I just made that up in my head. Not near Eclipse or Netbeans to make sure it actually works.

EDIT: Actually that may just be the algorithm for finding the smallest one. I really am too tired of java right now with projects and finals to think straight. :p
 
Status
Not open for further replies.
Back
Top Bottom