daily pastebin goal
58%
SHARE
TWEET

hw4

a guest Dec 9th, 2018 63 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /**
  2.  * Izaaz Izzadeen, 12/8, lab section - Tuesday, 8:45, Josue Ruiz
  3.  * Homework 4 Arrays and Objects
  4.  * This program creates and changes array lists.
  5.  **/
  6. public class MyArrayListStatic
  7. {
  8.    private static int[] a = {2,8,5,4,6};
  9.    public static void update(int index, int value)
  10.    {
  11.       a[index] = value;
  12.    }
  13.    
  14.    public static void add(int value)
  15.    {
  16.       int[] temp = new int[a.length+1];
  17.       for (int i=0;i<a.length;i++)
  18.         temp[i] = a[i];
  19.       temp[a.length] = value;
  20.       a = temp;
  21.    }
  22.    
  23.     public static void insert(int index, int value)
  24.    {
  25.        int[] temp = new int[a.length+1];
  26.        for(int i=0;i<index;i++)
  27.          temp[i] = a[i];
  28.        temp[index] = value;
  29.        for(int i=index;i<a.length;i++)
  30.          temp[i+1] = a[i];
  31.        a = temp;
  32.    }
  33.    
  34.     public static void delete(int index)
  35.    {
  36.        int[] temp = new int[a.length-1];
  37.        for(int i=0;i<index;i++)
  38.          temp[i] = a[i];
  39.        for(int i=index+1;i<a.length;i++)
  40.          temp[i-1] = a[i];
  41.        a = temp;
  42.    }
  43.    
  44.     public static void print()
  45.    {
  46.       for(int i=0;i<a.length;i++)
  47.         System.out.println("array["+i+"]: "+a[i]);  
  48.    }
  49. }
  50.  
  51.  
  52. /**
  53.  * Izaaz Izzadeen, 12/8, lab section - Tuesday, 8:45, Josue Ruiz
  54.  * Homework 4 Arrays and Objects
  55.  * This program creates and changes array lists.
  56.  **/
  57. public class HW4Static
  58. {
  59.  
  60.      public static void main(String[] args)
  61.    {
  62.      
  63.      //Array    
  64.      System.out.println("---------------------");    
  65.      System.out.println("Initial Array");
  66.      MyArrayListStatic.print();
  67.      
  68.      System.out.println("---------------------");    
  69.      System.out.println("Update Array: Change value in index 2 to 7");
  70.      MyArrayListStatic.update(2, 7); //Change value in index 2 changes from 5 to 7
  71.      MyArrayListStatic.print();
  72.      
  73.      System.out.println("---------------------");    
  74.      System.out.println("Add value 3 to the end of the array");
  75.      MyArrayListStatic.add(3); //Add value 3 to the end of the array
  76.      MyArrayListStatic.print();
  77.      
  78.      System.out.println("---------------------");    
  79.      System.out.println("Insert value 9 into index 3");
  80.      MyArrayListStatic.insert(3,9); //Insert value 9 into index 3
  81.      MyArrayListStatic.print();
  82.      
  83.      System.out.println("---------------------");    
  84.      System.out.println("Delete the value in index 3");
  85.      MyArrayListStatic.delete(3); //Delete the value in index 3
  86.      MyArrayListStatic.print();
  87.      
  88.    }
  89.  
  90.    
  91. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top