<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="ru">
		<id>http://jexp.ru/index.php?action=history&amp;feed=atom&amp;title=Java_Tutorial%2FCollections%2FArray_Copy_Clone</id>
		<title>Java Tutorial/Collections/Array Copy Clone - История изменений</title>
		<link rel="self" type="application/atom+xml" href="http://jexp.ru/index.php?action=history&amp;feed=atom&amp;title=Java_Tutorial%2FCollections%2FArray_Copy_Clone"/>
		<link rel="alternate" type="text/html" href="http://jexp.ru/index.php?title=Java_Tutorial/Collections/Array_Copy_Clone&amp;action=history"/>
		<updated>2026-04-11T04:50:02Z</updated>
		<subtitle>История изменений этой страницы в вики</subtitle>
		<generator>MediaWiki 1.30.0</generator>

	<entry>
		<id>http://jexp.ru/index.php?title=Java_Tutorial/Collections/Array_Copy_Clone&amp;diff=4740&amp;oldid=prev</id>
		<title>Admin: 1 версия</title>
		<link rel="alternate" type="text/html" href="http://jexp.ru/index.php?title=Java_Tutorial/Collections/Array_Copy_Clone&amp;diff=4740&amp;oldid=prev"/>
				<updated>2010-06-01T05:05:29Z</updated>
		
		<summary type="html">&lt;p&gt;1 версия&lt;/p&gt;
&lt;table class=&quot;diff diff-contentalign-left&quot; data-mw=&quot;interface&quot;&gt;
				&lt;tr style=&quot;vertical-align: top;&quot; lang=&quot;ru&quot;&gt;
				&lt;td colspan=&quot;1&quot; style=&quot;background-color: white; color:black; text-align: center;&quot;&gt;← Предыдущая&lt;/td&gt;
				&lt;td colspan=&quot;1&quot; style=&quot;background-color: white; color:black; text-align: center;&quot;&gt;Версия 05:05, 1 июня 2010&lt;/td&gt;
				&lt;/tr&gt;&lt;tr&gt;&lt;td colspan=&quot;2&quot; style=&quot;text-align: center;&quot; lang=&quot;ru&quot;&gt;&lt;div class=&quot;mw-diff-empty&quot;&gt;(нет различий)&lt;/div&gt;
&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;</summary>
		<author><name>Admin</name></author>	</entry>

	<entry>
		<id>http://jexp.ru/index.php?title=Java_Tutorial/Collections/Array_Copy_Clone&amp;diff=4739&amp;oldid=prev</id>
		<title> в 17:44, 31 мая 2010</title>
		<link rel="alternate" type="text/html" href="http://jexp.ru/index.php?title=Java_Tutorial/Collections/Array_Copy_Clone&amp;diff=4739&amp;oldid=prev"/>
				<updated>2010-05-31T17:44:27Z</updated>
		
		<summary type="html">&lt;p&gt;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Новая страница&lt;/b&gt;&lt;/p&gt;&lt;div&gt;==  Array clone ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
  &amp;lt;!-- start source code --&amp;gt;&lt;br /&gt;
   &lt;br /&gt;
    &amp;lt;source lang=&amp;quot;java&amp;quot;&amp;gt;&lt;br /&gt;
public class MainClass {&lt;br /&gt;
  public static void main (String args[]) {&lt;br /&gt;
    int array1[] = {1, 2, 3, 4, 5};&lt;br /&gt;
    int[] clone = (int[]) array1.clone();    &lt;br /&gt;
    for(int i: clone){&lt;br /&gt;
      System.out.println(i);  &lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
  }&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
    &lt;br /&gt;
   &lt;br /&gt;
  &amp;lt;!-- end source code --&amp;gt;&lt;br /&gt;
   &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=codeResult&amp;gt;1&lt;br /&gt;
2&lt;br /&gt;
3&lt;br /&gt;
4&lt;br /&gt;
5&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==  Copies the given array and adds the given element at the end of the new array. (long value type) ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
  &amp;lt;!-- start source code --&amp;gt;&lt;br /&gt;
   &lt;br /&gt;
    &amp;lt;source lang=&amp;quot;java&amp;quot;&amp;gt;&lt;br /&gt;
/*   Copyright 2004 The Apache Software Foundation&lt;br /&gt;
 *&lt;br /&gt;
 *   Licensed under the Apache License, Version 2.0 (the &amp;quot;License&amp;quot;);&lt;br /&gt;
 *   you may not use this file except in compliance with the License.&lt;br /&gt;
 *   You may obtain a copy of the License at&lt;br /&gt;
 *&lt;br /&gt;
 *       http://www.apache.org/licenses/LICENSE-2.0&lt;br /&gt;
 *&lt;br /&gt;
 *   Unless required by applicable law or agreed to in writing, software&lt;br /&gt;
 *   distributed under the License is distributed on an &amp;quot;AS IS&amp;quot; BASIS,&lt;br /&gt;
 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.&lt;br /&gt;
 *   See the License for the specific language governing permissions and&lt;br /&gt;
 *  limitations under the License.&lt;br /&gt;
 */&lt;br /&gt;
&lt;br /&gt;
import java.lang.reflect.Array;&lt;br /&gt;
&lt;br /&gt;
/**&lt;br /&gt;
 * &amp;lt;p&amp;gt;Operations on arrays, primitive arrays (like &amp;lt;code&amp;gt;int[]&amp;lt;/code&amp;gt;) and&lt;br /&gt;
 * primitive wrapper arrays (like &amp;lt;code&amp;gt;Integer[]&amp;lt;/code&amp;gt;).&amp;lt;/p&amp;gt;&lt;br /&gt;
 * &lt;br /&gt;
 * &amp;lt;p&amp;gt;This class tries to handle &amp;lt;code&amp;gt;null&amp;lt;/code&amp;gt; input gracefully.&lt;br /&gt;
 * An exception will not be thrown for a &amp;lt;code&amp;gt;null&amp;lt;/code&amp;gt;&lt;br /&gt;
 * array input. However, an Object array that contains a &amp;lt;code&amp;gt;null&amp;lt;/code&amp;gt;&lt;br /&gt;
 * element may throw an exception. Each method documents its behaviour.&amp;lt;/p&amp;gt;&lt;br /&gt;
 *&lt;br /&gt;
 * @author Stephen Colebourne&lt;br /&gt;
 * @author Moritz Petersen&lt;br /&gt;
 * @author &lt;br /&gt;
 * @author Maarten Coene&lt;br /&gt;
 * @since 2.0&lt;br /&gt;
 * @version $Id: ArrayUtils.java 632503 2008-03-01 00:21:52Z ggregory $&lt;br /&gt;
 */&lt;br /&gt;
public class Main {&lt;br /&gt;
  &lt;br /&gt;
  /**&lt;br /&gt;
   * &amp;lt;p&amp;gt;Copies the given array and adds the given element at the end of the new array.&amp;lt;/p&amp;gt;&lt;br /&gt;
   *&lt;br /&gt;
   * &amp;lt;p&amp;gt;The new array contains the same elements of the input&lt;br /&gt;
   * array plus the given element in the last position. The component type of &lt;br /&gt;
   * the new array is the same as that of the input array.&amp;lt;/p&amp;gt;&lt;br /&gt;
   *&lt;br /&gt;
   * &amp;lt;p&amp;gt;If the input array is &amp;lt;code&amp;gt;null&amp;lt;/code&amp;gt;, a new one element array is returned&lt;br /&gt;
   *  whose component type is the same as the element.&amp;lt;/p&amp;gt;&lt;br /&gt;
   * &lt;br /&gt;
   * &amp;lt;pre&amp;gt;&lt;br /&gt;
   * ArrayUtils.add(null, 0)   = [0]&lt;br /&gt;
   * ArrayUtils.add([1], 0)    = [1, 0]&lt;br /&gt;
   * ArrayUtils.add([1, 0], 1) = [1, 0, 1]&lt;br /&gt;
   * &amp;lt;/pre&amp;gt;&lt;br /&gt;
   * &lt;br /&gt;
   * @param array  the array to copy and add the element to, may be &amp;lt;code&amp;gt;null&amp;lt;/code&amp;gt;&lt;br /&gt;
   * @param element  the object to add at the last index of the new array&lt;br /&gt;
   * @return A new array containing the existing elements plus the new element&lt;br /&gt;
   * @since 2.1&lt;br /&gt;
   */&lt;br /&gt;
  public static long[] add(long[] array, long element) {&lt;br /&gt;
      long[] newArray = (long[])copyArrayGrow1(array, Long.TYPE);&lt;br /&gt;
      newArray[newArray.length - 1] = element;&lt;br /&gt;
      return newArray;&lt;br /&gt;
  }&lt;br /&gt;
  &lt;br /&gt;
  /**&lt;br /&gt;
   * Returns a copy of the given array of size 1 greater than the argument. &lt;br /&gt;
   * The last value of the array is left to the default value.&lt;br /&gt;
   * &lt;br /&gt;
   * @param array The array to copy, must not be &amp;lt;code&amp;gt;null&amp;lt;/code&amp;gt;.&lt;br /&gt;
   * @param newArrayComponentType If &amp;lt;code&amp;gt;array&amp;lt;/code&amp;gt; is &amp;lt;code&amp;gt;null&amp;lt;/code&amp;gt;, create a &lt;br /&gt;
   * size 1 array of this type.&lt;br /&gt;
   * @return A new copy of the array of size 1 greater than the input.&lt;br /&gt;
   */    &lt;br /&gt;
  private static Object copyArrayGrow1(Object array, Class newArrayComponentType) {&lt;br /&gt;
      if (array != null) {&lt;br /&gt;
          int arrayLength = Array.getLength(array);&lt;br /&gt;
          Object newArray = Array.newInstance(array.getClass().getComponentType(), arrayLength + 1);&lt;br /&gt;
          System.arraycopy(array, 0, newArray, 0, arrayLength);&lt;br /&gt;
          return newArray;&lt;br /&gt;
      }&lt;br /&gt;
      return Array.newInstance(newArrayComponentType, 1);&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
    &lt;br /&gt;
   &lt;br /&gt;
  &amp;lt;!-- end source code --&amp;gt;&lt;br /&gt;
   &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==  Copying and Cloning Arrays ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
  &amp;lt;!-- start source code --&amp;gt;&lt;br /&gt;
   &lt;br /&gt;
    &amp;lt;source lang=&amp;quot;java&amp;quot;&amp;gt;&lt;br /&gt;
public class MainClass {&lt;br /&gt;
  public static void main (String args[]) {&lt;br /&gt;
    int array1[] = {1, 2, 3, 4, 5};&lt;br /&gt;
    int array2[] = {1, 2, 3, 4, 5, 6, 7, 8, 9};&lt;br /&gt;
    System.out.println(&amp;quot;Original size: &amp;quot; + array1.length);&lt;br /&gt;
    System.out.println(&amp;quot;New size: &amp;quot; + doubleArray(array1).length);&lt;br /&gt;
    System.out.println(&amp;quot;Original size: &amp;quot; + array2.length);&lt;br /&gt;
    System.out.println(&amp;quot;New size: &amp;quot; + doubleArray(array2).length);&lt;br /&gt;
  }&lt;br /&gt;
  static int[] doubleArray(int original[]) {&lt;br /&gt;
    int length = original.length;&lt;br /&gt;
    int newArray[] = new int[length*2];&lt;br /&gt;
    System.arraycopy(original, 0, newArray, 0, length);&lt;br /&gt;
    return newArray;&lt;br /&gt;
  }&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
    &lt;br /&gt;
   &lt;br /&gt;
  &amp;lt;!-- end source code --&amp;gt;&lt;br /&gt;
   &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=codeResult&amp;gt;Original size: 5&lt;br /&gt;
New size: 10&lt;br /&gt;
Original size: 9&lt;br /&gt;
New size: 18&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==  Copy some items of an array into another array ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
  &amp;lt;!-- start source code --&amp;gt;&lt;br /&gt;
   &lt;br /&gt;
    &amp;lt;source lang=&amp;quot;java&amp;quot;&amp;gt;&lt;br /&gt;
public class Main {&lt;br /&gt;
  public static void main(String[] args) {&lt;br /&gt;
    String[] letters = { &amp;quot;A&amp;quot;, &amp;quot;I&amp;quot;, &amp;quot;U&amp;quot;, &amp;quot;E&amp;quot;, &amp;quot;O&amp;quot; };&lt;br /&gt;
    String[] results = new String[3];&lt;br /&gt;
    System.arraycopy(letters, 2, results, 0, 3);&lt;br /&gt;
    for (int i = 0; i &amp;lt; results.length; i++) {&lt;br /&gt;
      System.out.println(&amp;quot;result = &amp;quot; + results[i]);&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
    &lt;br /&gt;
   &lt;br /&gt;
  &amp;lt;!-- end source code --&amp;gt;&lt;br /&gt;
   &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==  Doubling the size of an array ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
  &amp;lt;!-- start source code --&amp;gt;&lt;br /&gt;
   &lt;br /&gt;
    &amp;lt;source lang=&amp;quot;java&amp;quot;&amp;gt;&lt;br /&gt;
public class MainClass {&lt;br /&gt;
  public static void main (String args[]) {&lt;br /&gt;
    int array1[] = {1, 2, 3, 4, 5};&lt;br /&gt;
    int array2[] = {1, 2, 3, 4, 5, 6, 7, 8, 9};&lt;br /&gt;
    System.out.println(&amp;quot;Original size: &amp;quot; + array1.length);&lt;br /&gt;
    System.out.println(&amp;quot;New size: &amp;quot; + doubleArray(array1).length);&lt;br /&gt;
    System.out.println(&amp;quot;Original size: &amp;quot; + array2.length);&lt;br /&gt;
    System.out.println(&amp;quot;New size: &amp;quot; + doubleArray(array2).length);&lt;br /&gt;
  }&lt;br /&gt;
  static int[] doubleArray(int original[]) {&lt;br /&gt;
    int length = original.length;&lt;br /&gt;
    int newArray[] = new int[length*2];&lt;br /&gt;
    System.arraycopy(original, 0, newArray, 0, length);&lt;br /&gt;
    return newArray;&lt;br /&gt;
  }&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
    &lt;br /&gt;
   &lt;br /&gt;
  &amp;lt;!-- end source code --&amp;gt;&lt;br /&gt;
   &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=codeResult&amp;gt;Original size: 5&lt;br /&gt;
New size: 10&lt;br /&gt;
Original size: 9&lt;br /&gt;
New size: 18&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==  use Arrays.copyOf to copy array ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
  &amp;lt;!-- start source code --&amp;gt;&lt;br /&gt;
   &lt;br /&gt;
    &amp;lt;source lang=&amp;quot;java&amp;quot;&amp;gt;&lt;br /&gt;
import java.util.Arrays;&lt;br /&gt;
class MainClass&lt;br /&gt;
{&lt;br /&gt;
    public static void main(String args[])&lt;br /&gt;
    {&lt;br /&gt;
        int arrayOriginal[] = {42, 55, 21};&lt;br /&gt;
        int arrayNew[] =&lt;br /&gt;
            Arrays.copyOf(arrayOriginal, 3);             &lt;br /&gt;
        printIntArray(arrayNew);&lt;br /&gt;
    }&lt;br /&gt;
    static void printIntArray(int arrayNew[])&lt;br /&gt;
    {&lt;br /&gt;
        for (int i : arrayNew)&lt;br /&gt;
        {&lt;br /&gt;
            System.out.print(i);&lt;br /&gt;
            System.out.print(&amp;quot; &amp;quot;);&lt;br /&gt;
        }&lt;br /&gt;
        System.out.println();&lt;br /&gt;
    }&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
    &lt;br /&gt;
   &lt;br /&gt;
  &amp;lt;!-- end source code --&amp;gt;&lt;br /&gt;
   &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==  Using Arrays.copyOf to copy an array ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
  &amp;lt;!-- start source code --&amp;gt;&lt;br /&gt;
   &lt;br /&gt;
    &amp;lt;source lang=&amp;quot;java&amp;quot;&amp;gt;&lt;br /&gt;
import java.util.Arrays;&lt;br /&gt;
public class ArrayCopy {&lt;br /&gt;
  public static void main(String args[]) {&lt;br /&gt;
    System.out.printf(&amp;quot;Before (original)\t%s%n&amp;quot;, Arrays.toString(args));&lt;br /&gt;
    String copy[] = Arrays.copyOf(args, 4);&lt;br /&gt;
    System.out.printf(&amp;quot;Before (copy)\t\t%s%n&amp;quot;, Arrays.toString(copy));&lt;br /&gt;
    copy[0] = &amp;quot;A&amp;quot;;&lt;br /&gt;
    copy[1] = &amp;quot;B&amp;quot;;&lt;br /&gt;
    copy[2] = &amp;quot;C&amp;quot;;&lt;br /&gt;
    copy[3] = &amp;quot;D&amp;quot;;&lt;br /&gt;
    System.out.printf(&amp;quot;After (original)\t%s%n&amp;quot;, Arrays.toString(args));&lt;br /&gt;
    System.out.printf(&amp;quot;After (copy)\t\t%s%n&amp;quot;, Arrays.toString(copy));&lt;br /&gt;
  }&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
    &lt;br /&gt;
   &lt;br /&gt;
  &amp;lt;!-- end source code --&amp;gt;&lt;/div&gt;</summary>
			</entry>

	</feed>