Advertisement
Guest User

Untitled

a guest
Feb 27th, 2020
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.92 KB | None | 0 0
  1. import java.util.ArrayList;
  2.  
  3. /**
  4.  * Write a description of class ArrayPractice here.
  5.  *
  6.  * @author (your name)
  7.  * @version (a version number or a date)
  8.  */
  9. public class ArrayPractice
  10. {
  11.     public static void main(String[] args) {
  12.        one();
  13.        two();
  14.     }
  15.    
  16.     public static void one() {
  17.         int[] x = {3,4,5,5,6};
  18.         //int[] x = new int[5];
  19.        
  20.         int[][] y = { {3,4,5,6},
  21.                       {7,6,4,3,3,2,2,2,3},
  22.                       {1,2,3,4,-5,6,7,7,45,4}
  23.                     };
  24.                    
  25.         for (int[] row : y) {
  26.             for (int val : row) {
  27.                 System.out.print(val + " ");
  28.             }
  29.             System.out.println();
  30.         }
  31.  
  32.         //int g = 33, r = 55, w = 0;  // g is..., r is ... , w = ....
  33.        
  34.         for (int i = 0, j = 3; i < 10; i += 1) {
  35.            
  36.         }
  37.        
  38.         boolean finished = false;
  39.        
  40.         for (int row = 0; !finished && row < y.length; row += 1) {
  41.            
  42.             for (int col = 0; !finished && col < y[row].length; col += 1) {
  43.                 if (y[row][col] < 0) {
  44.                     finished = true;
  45.                 }
  46.                 else {
  47.                     System.out.print(y[row][col] + " ");
  48.                 }
  49.             }
  50.             System.out.println();
  51.         }
  52.         if (finished) {
  53.                 System.out.println("early exit, negative found");
  54.             }
  55.         else {
  56.                 System.out.println("no negative");
  57.             }
  58.  
  59.                    
  60.         String[][] s = { {"abc"},
  61.                          {"bob", "ertt"},
  62.                          null
  63.                        };
  64.         String[] ss = {"abc", "def", "hig"};
  65.         s[2] = ss;
  66.         System.out.println( s[2].length);
  67.     }
  68.    
  69.     public static void two() {
  70.         int[][] x = new int[10][3];
  71.         int[] t = {1,2,3};
  72.         x[0] = t;
  73.         x[1] = t;
  74.         x[2] = t;
  75.         t = new int[10];
  76.         x[2] = t;
  77.         t[1] = 99;
  78.         print2Dint(x);
  79.     }
  80.    
  81.     public static void three() {
  82.         ArrayList<ArrayList<Integer>> twoD = new ArrayList<>();
  83.         ArrayList<int[]> demo = new ArrayList<>();
  84.         ArrayList<Integer> x = new ArrayList<>();
  85.         x.add(1); x.add(2); x.add(3);
  86.         twoD.add(x);
  87.         x = new ArrayList<>();
  88.         x.add(34); x.add(23);
  89.         twoD.add(x);
  90.         print2DLists(twoD);
  91.     }
  92.     public static void print2DLists(ArrayList<ArrayList<Integer>> y) {
  93.         for (ArrayList<Integer> row : y) {
  94.             for (int val : row) {
  95.                 System.out.print(val + " ");
  96.             }
  97.             System.out.println();
  98.         }
  99.     }    
  100.     public static void print2Dint(int[][] y) {
  101.         for (int[] row : y) {
  102.             for (int val : row) {
  103.                 System.out.print(val + " ");
  104.             }
  105.             System.out.println();
  106.         }
  107.     }
  108.  
  109. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement