Advertisement
makispaiktis

I5. Collections methods - asList, reverse, copy, fill

Jun 6th, 2022 (edited)
1,293
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.91 KB | None | 0 0
  1. import java.util.*;
  2.  
  3. public class myClass {
  4.  
  5.     public static void main(String[] args){
  6.  
  7.         // Create an array and convert to list
  8.         Character[] ray = {'p', 'w', 'n', 's'};
  9.         List<Character> l = Arrays.asList(ray);
  10.         System.out.println("Original : ");
  11.         System.out.println(l);
  12.         // Reverse and print list
  13.         Collections.reverse(l);
  14.         System.out.println("Reverse : ");
  15.         System.out.println(l);
  16.         // Create a new array and a new list
  17.         Character[] newRay = new Character[l.size()];
  18.         List<Character> listCopy = Arrays.asList(newRay);    // Blank list with predetermined size
  19.         Collections.copy(listCopy, l);
  20.         System.out.println("After copy : ");
  21.         System.out.println(listCopy);
  22.         // Fill
  23.         Collections.fill(l, 'X');
  24.         System.out.println("Fill with 'X' : ");
  25.         System.out.println(l);
  26.  
  27.     }
  28. }
  29.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement