Java by API/java.lang.reflect/Array

Материал из Java эксперт
Перейти к: навигация, поиск

Array: clone()

   <source lang="java">
 

/*

Output:

Original size: 5 New size: 5 Original size: 9 New size: 9

* */

public class MainClass {

 public static void main (String args[]) {
   int array1[] = {1, 2, 3, 4, 5};
   int array2[] = {1, 2, 3, 4, 5, 6, 7, 8, 9};
   System.out.println("Original size: " + array1.length);
   System.out.println("New size: " + cloneArray(array1).length);
   System.out.println("Original size: " + array2.length);
   System.out.println("New size: " + cloneArray(array2).length);
 }
 static int[] cloneArray(int original[]) {
   return (int[])original.clone();
 }

}


 </source>
   
  
 
  



Array: getInt(Object array, int index)

   <source lang="java">
 

/*

Output:

Position: 0, value: 1040988893 Position: 1, value: -2073064706 Position: 2, value: -808861084

* */

import java.lang.reflect.Array; import java.util.Random; public class MainClass {

 public static void main (String args[]) {
   Object array = Array.newInstance(int.class, 3);
   
   
   int length = Array.getLength(array);
   Random generator = new Random(System.currentTimeMillis());
   for (int i=0; i<length; i++) {
     int random = generator.nextInt();
     Array.setInt(array, i, random);
   }
   
   for (int i=0; i<length; i++) {
     int value = Array.getInt(array, i);
     System.out.println("Position: " + i + ", value: " + value);
   }
 }

}


 </source>
   
  
 
  



Array: getLength(Object array)

   <source lang="java">
 

/*

Output:

Array of: int Array size: 3

* */

import java.lang.reflect.Array; public class MainClass {

 public static void main (String args[]) {
   Object array = Array.newInstance(int.class, 3);
   
   Class type = array.getClass();
   if (type.isArray()) {
     Class elementType = type.getComponentType();
     System.out.println("Array of: " + elementType);
     System.out.println("Array size: " + Array.getLength(array));
   }
   
 }

}


 </source>
   
  
 
  



Array: get(Object array, int index)

   <source lang="java">
 

import java.lang.reflect.Array; public class Main {

 public static Object copyArray(final Object input) {
   final Class type = input.getClass();
   if (!type.isArray()) {
     throw new IllegalArgumentException();
   }
   final int length = Array.getLength(input);
   final Class componentType = type.getComponentType();
   final Object result = Array.newInstance(componentType, length);
   for (int idx = 0; idx < length; idx++) {
     Array.set(result, idx, Array.get(input, idx));
   }
   return result;
 }
 public static void main(final String[] args) {
   try {
     int[] x = new int[] { 2, 3, 8, 7, 5 };
     char[] y = new char[] { "a", "z", "e" };
     String[] z = new String[] { "Jim", "John", "Joe" };
     System.out.println(" -- z and copy of z --");
     outputArrays(z, copyArray(z));
   } catch (final Exception ex) {
     ex.printStackTrace();
   }
 }
 public static void outputArrays(final Object first, final Object second) {
   if (!first.getClass().isArray()) {
     throw new IllegalArgumentException("first is not an array.");
   }
   if (!second.getClass().isArray()) {
     throw new IllegalArgumentException("second is not an array.");
   }
   final int lengthFirst = Array.getLength(first);
   final int lengthSecond = Array.getLength(second);
   final int length = Math.max(lengthFirst, lengthSecond);
   for (int idx = 0; idx < length; idx++) {
     System.out.print("[" + idx + "]\t");
     if (idx < lengthFirst) {
       System.out.print(Array.get(first, idx) + "\t\t");
     } else {
       System.out.print("\t\t");
     }
     if (idx < lengthSecond) {
       System.out.print(Array.get(second, idx) + "\t");
     }
     System.out.println();
   }
 }

}


 </source>
   
  
 
  



Array: newInstance(Class<?> componentType, int... dimensions)

   <source lang="java">

import java.lang.reflect.Array; public class Main {

 public static void main(String[] argv) throws Exception {
   int[][] ints2 = (int[][]) Array.newInstance(int.class, new int[] { 10, 20 });
 }

}

 </source>
   
  
 
  



Array: newInstance(Class componentType, int length)

   <source lang="java">
 

/*

Output:

Array of: int Array size: 3

* */

import java.lang.reflect.Array; public class MainClass {

 public static void main (String args[]) {
   Object array = Array.newInstance(int.class, 3);
   
   Class type = array.getClass();
   if (type.isArray()) {
     Class elementType = type.getComponentType();
     System.out.println("Array of: " + elementType);
     System.out.println("Array size: " + Array.getLength(array));
   }
   
 }

}


 </source>
   
  
 
  



Array: setInt(Object array, int index, int i)

   <source lang="java">
 

/*

Output:

Position: 0, value: 1040988893 Position: 1, value: -2073064706 Position: 2, value: -808861084

* */

import java.lang.reflect.Array; import java.util.Random; public class MainClass {

 public static void main (String args[]) {
   Object array = Array.newInstance(int.class, 3);
   
   
   int length = Array.getLength(array);
   Random generator = new Random(System.currentTimeMillis());
   for (int i=0; i<length; i++) {
     int random = generator.nextInt();
     Array.setInt(array, i, random);
   }
   
   for (int i=0; i<length; i++) {
     int value = Array.getInt(array, i);
     System.out.println("Position: " + i + ", value: " + value);
   }
 }

}


 </source>
   
  
 
  



Array: setLong(Object array, int index, long l)

   <source lang="java">
 

import java.lang.reflect.Array; public class Main {

 public static void main(String... args) {
   Object o = new int[2];
   Array.setShort(o, 0, (short) 2); // widening, succeeds
   Array.setLong(o, 1, 2L); // narrowing, fails
 }

}


 </source>
   
  
 
  



Array: set(Object array, int index, Object value)

   <source lang="java">

import java.lang.reflect.Array; public class Main {

 public static void main(String[] argv) throws Exception {
   int[] array = { 1, 2, 3 };
   // Get the value of the third element.
   Object o = Array.get(array, 2);
   // Set the value of the third element.
   Array.set(array, 2, 1);
 }

}

 </source>
   
  
 
  



Array: setShort(Object array, int index, short s)

   <source lang="java">
 

import java.lang.reflect.Array; public class Main {

 public static void main(String... args) {
   Object o = new int[2];
   Array.setShort(o, 0, (short) 2); // widening, succeeds
   Array.setLong(o, 1, 2L); // narrowing, fails
 }

}


 </source>