Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Izaaz Izzadeen, 12/8, lab section - Tuesday, 8:45, Josue Ruiz
- * Homework 4 Arrays and Objects
- * This program creates and changes array lists.
- **/
- public class MyArrayListStatic
- {
- private static int[] a = {2,8,5,4,6};
- public static void update(int index, int value)
- {
- a[index] = value;
- }
- public static void add(int value)
- {
- int[] temp = new int[a.length+1];
- for (int i=0;i<a.length;i++)
- temp[i] = a[i];
- temp[a.length] = value;
- a = temp;
- }
- public static void insert(int index, int value)
- {
- int[] temp = new int[a.length+1];
- for(int i=0;i<index;i++)
- temp[i] = a[i];
- temp[index] = value;
- for(int i=index;i<a.length;i++)
- temp[i+1] = a[i];
- a = temp;
- }
- public static void delete(int index)
- {
- int[] temp = new int[a.length-1];
- for(int i=0;i<index;i++)
- temp[i] = a[i];
- for(int i=index+1;i<a.length;i++)
- temp[i-1] = a[i];
- a = temp;
- }
- public static void print()
- {
- for(int i=0;i<a.length;i++)
- System.out.println("array["+i+"]: "+a[i]);
- }
- }
- /**
- * Izaaz Izzadeen, 12/8, lab section - Tuesday, 8:45, Josue Ruiz
- * Homework 4 Arrays and Objects
- * This program creates and changes array lists.
- **/
- public class HW4Static
- {
- public static void main(String[] args)
- {
- //Array
- System.out.println("---------------------");
- System.out.println("Initial Array");
- MyArrayListStatic.print();
- System.out.println("---------------------");
- System.out.println("Update Array: Change value in index 2 to 7");
- MyArrayListStatic.update(2, 7); //Change value in index 2 changes from 5 to 7
- MyArrayListStatic.print();
- System.out.println("---------------------");
- System.out.println("Add value 3 to the end of the array");
- MyArrayListStatic.add(3); //Add value 3 to the end of the array
- MyArrayListStatic.print();
- System.out.println("---------------------");
- System.out.println("Insert value 9 into index 3");
- MyArrayListStatic.insert(3,9); //Insert value 9 into index 3
- MyArrayListStatic.print();
- System.out.println("---------------------");
- System.out.println("Delete the value in index 3");
- MyArrayListStatic.delete(3); //Delete the value in index 3
- MyArrayListStatic.print();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement