Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class MainClass {
- public static void main(String[] args){
- String[] names1 = mkArr("Hero1", "Hero2", "Hero3");
- String[] names2 = mkArr("Villain1", "Villain2", "Villain3");
- //Prints Heroes first, then the villains
- pArr(names1);
- pArr(names2);
- //If it were pass-by-ref - then this would print
- // villains first, then heroes, yet it doesn't
- swapArr(names1,names2);
- pArr(names1);
- pArr(names2);
- }
- static void swapArr(Object[] a1, Object[] a2){
- Object[] t = a1;
- a1 = a2;
- a2 = t;
- System.out.println("[START]Dbg swapArr res:");
- pArr(a1); //villains
- pArr(a2); //heroes
- System.out.println("[END] Dbg swapArr res:");
- }
- static <T> void pArr(T[] a){
- System.out.print('{');
- for(T t : a){
- System.out.print(t.toString()+",");
- }
- System.out.println('}');
- }
- static <T> T[] mkArr(T... elements){
- return elements;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment