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
Here's the rest of the code if that matters
I'm sure this is just simple easy stuff But it's a start I guess lol
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
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 But it's a start I guess lol