Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.ArrayList;
- import java.util.Queue;
- public class PriorityProject {
- //a large ArrayList to hold the information once it has been prioritized
- ArrayList<String> mrah = new ArrayList<String>();
- //constructor
- public PriorityProject(ArrayList moose){
- mrah = moose;
- }
- //precondition: mrah exists, and a string name and an int priority exist
- //postcondition: the string and int are combined into a string and added to the ArrayList mrah
- public void enqueue(String name, int priority) {
- String stuffs = (name + priority);
- mrah.add(stuffs);
- }
- //preconditon: mrah exists
- //postcondition: returnes what is about to be removed, before it is removed
- public String dequeuereturn() {
- return mrah.get(0);
- }
- //precondition: mrah exists
- //postcondition: the first element in the ArrayList is removed
- public void dequeueremove() {
- mrah.remove(0);
- }
- //precondition: mrah exists
- //postcondition: if mrah has any elements it returns true, otherwise (since it is empty) it returns false
- public boolean isEmpty() {
- if (mrah.size() > -1) {
- return true;
- } else {
- return false;
- }
- }
- //precondition: mrah (an ArrayList) exists
- //postcondition: returns the first element inputted into the ArrayList
- public String front() {
- return mrah.get(0);
- }
- public static void main(String[] args) {
- //An ArrayList to hold the city's name
- ArrayList<String> words = new ArrayList<String>();
- //an ArrayList that takes in the name and priority number as one string and holds it until the String and int can be separated
- ArrayList<String> everything = new ArrayList<String>();
- //an ArrayList to be passed to the constructor. Does the same thing as ArrayList mrah
- ArrayList <String> thing = new ArrayList <String> ();
- //the information is added
- everything.add("Columbus , 3");// 1
- everything.add("New York , 5");// 2
- everything.add("Baltimore , 4");// 3
- everything.add("Greencastle , 1");// 4
- everything.add("Washington , 5");// 5
- everything.add("Atlanta , 4");// 6
- everything.add("Seneca , 1");// 7
- everything.add("Frederick , 3");// 8
- everything.add("Columbia , 3");// 9
- everything.add("Dallas , 4");// 10
- everything.add("Los Angeles , 5");// 11
- // separates the string from the priority level
- for (int i = 0; i < everything.size(); i++) {
- //takes in the first line and divides it into an array of Characters
- char[] array = everything.get(i).toCharArray();
- //goes through the arrya of characters
- for (int j = 0; j < array.length; j++) {
- //when a comma is seen...
- if (array[j] == (',')) {
- //the array of Characters is turned back into a String
- String str = new String(array);
- //then, the string is reduced to everything BEFORE the comma, and held in ArrayList words
- str = str.substring(0, j);
- words.add(str);
- }
- }
- }
- PriorityProject p = new PriorityProject(thing);
- // separates the priority level from the string
- for (int c = 5; c > 0; c--) {
- //does essentially the same thing as the above for loop
- for (int y = 0; y < everything.size(); y++) {
- char[] arrays = everything.get(y).toCharArray();
- for (int j = 0; j < arrays.length; j++) {
- if (arrays[j] == (',')) {
- //except here it takes the character two spaces away from the comma(which is the number)
- Character ch = arrays[j + 2];
- //turns that character into a string
- String s = ch.toString();
- //parses that string into an int (there doesnt seem to be a way to go directly from Char to Int)
- int x = Integer.parseInt(s);
- if (x == c) {
- p.enqueue(words.get(y), x);
- }
- }
- }
- }
- }
- //prints out the now prioritized ArrayList
- for (int h =0; h<thing.size(); h++){
- System.out.println(thing.get(h));
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement