// advancedarrays.java

import javax.swing.JOptionPane;

import java.util.*;

public class advancedarrays
{
    // an example of a 2-dimensional array    
    public static void multi() {
        // fake that we're making a 2-d array to represent a 1024x768 display
        int[][] screen = new int[1024][768];
        // loop through every column (keep in mind screen is actually an array
        // of arrays, and screen.length will tell you how many arrays are inside
        // of screen
        for (int a = 0; a < screen.length; a++) {
            // now loop through each individual array inside of screen
            // remember screen[a].length will tell you how large the ath screen
            // array is
            for (int b = 0; b < screen[a].length; b++) {
                // set this element to 50
                screen[a][b] = 50;
            }
        }
        // change the center "pixel" to 100
        screen[1024/2][768/2] = 100;
        
        for (int x = 0; x < screen.length; x++) {
            for (int y = 0; y < screen[x].length; y++) {
                // if we could draw, this could be a funky design
                screen[x][y] = x + y;
            }
        }
    }
    
    // show off an arraylist
    public static void readinarray() {
        // define the arraylist
        // note: to use ArrayLists we need to import java.util.*;
        ArrayList a = new ArrayList();
        // define a string to read in and set it to an empty string
        String input = "";
        // keep on reading until the user types in QUIT
        while (!input.equals("QUIT")) {
            // read in something
            input = JOptionPane.showInputDialog("Enter in a number, or type QUIT to quit");
            // add it to the end of the ArrayList.. if we wanted to put this at the start
            // of the arraylist, we would say a.add(0,input);, if we wanted to set the 1st
            // element to input, we'd say a.set(0,input);
            a.add(input);
        }
        // don't do this, but if we wanted to, we could do this: a.add(a);
        // define a variable to get the total, and set it to 0
        double total=0;
        // define the total items (make it 1 less than the total since the last item is
        // the word QUIT
        double totalitems = a.size()-1;
        for (int b = 0; b < totalitems; b++) {
            // add the string converted to a double to the running total
            // note, we have to cast the result of a.get(b) to a String since
            // as far as ArrayList is concerned, all items inside of it are
            // of type Object
            total += Double.parseDouble((String) a.get(b));
            }
        // now to show off we can remove items from the ArrayList
        for (int b = a.size(); b > 0; b--) {
            a.remove(0);
        }
        // show the result
        JOptionPane.showMessageDialog(null, "The mean is " + total/totalitems);
    }
    
    public static void sortarray() {
        // show off a very simple (Bubble Sort) sorting mechanism
        // define an array of 10 elements
        int[] arr = new int[10];
        // read in 10 numbers
        for (int a = 0; a < 10; a++) {
            arr[a] = Integer.parseInt(JOptionPane.showInputDialog("Gimme a number fool!"));
        }
        // define a variable for the purpose of swapping elements if we need to
        int temp;
        // read the book for descriptions on sorting
        for (int a = 9; a > 0; a--) {
            for (int b = 0; b < a; b++) {
                if (arr[b] > arr[b+1]) {
                    temp = arr[b+1];
                    arr[b+1] = arr[b];
                    arr[b] = temp;
                    }
                }
            }
        // now display the results
        for (int a = 0; a < 10; a++) {
            System.out.println(arr[a]);
            }
        }
}