using …

Pure C#

08 Nov 2008 için Arşiv

List<(Of )>)..::.ToArray Method

Yazan: esersahin 08/11/2008

http://msdn.microsoft.com/en-us/library/x303t819.aspx

Copies the elements of the List<(Of <(T>)>) to a new array.

Namespace: System.Collections.Generic
Assembly: mscorlib (in mscorlib.dll)

Visual Basic (Declaration)
Public Function ToArray As T()
Visual Basic (Usage)
Dim instance As List
Dim returnValue As T()

returnValue = instance.ToArray()
C#
public T[] ToArray()
Visual C++
public:
array<T>^ ToArray()
JScript
public function ToArray() : T[]

Return Value

Type: array<T>[]()[]
An array containing copies of the elements of the List<(Of <(T>)>).

The elements are copied using Array..::.Copy, which is an O(n) operation, where n is Count.

This method is an O(n) operation, where n is Count.

The following code example demonstrates the ToArray method and other methods of the List<(Of <(T>)>) class that act on ranges. At the end of the code example, the GetRange method is used to get three items from the list, beginning with index location 2. The ToArray method is called on the resulting List<(Of <(T>)>), creating an array of three elements. The elements of the array are displayed.

Visual Basic
Imports System
Imports System.Collections.Generic

Public Class Example

    Public Shared Sub Main()

        Dim input() As String = { "Brachiosaurus", _
                                  "Amargasaurus", _
                                  "Mamenchisaurus" }

        Dim dinosaurs As New List(Of String)(input)

        Console.WriteLine(vbLf & "Capacity: {0}", dinosaurs.Capacity)

        Console.WriteLine()
        For Each dinosaur As String In dinosaurs
            Console.WriteLine(dinosaur)
        Next

        Console.WriteLine(vbLf & "AddRange(dinosaurs)")
        dinosaurs.AddRange(dinosaurs)

        Console.WriteLine()
        For Each dinosaur As String In dinosaurs
            Console.WriteLine(dinosaur)
        Next

        Console.WriteLine(vbLf & "RemoveRange(2, 2)")
        dinosaurs.RemoveRange(2, 2)

        Console.WriteLine()
        For Each dinosaur As String In dinosaurs
            Console.WriteLine(dinosaur)
        Next

        input = New String() { "Tyrannosaurus", _
                               "Deinonychus", _
                               "Velociraptor" }

        Console.WriteLine(vbLf & "InsertRange(3, input)")
        dinosaurs.InsertRange(3, input)

        Console.WriteLine()
        For Each dinosaur As String In dinosaurs
            Console.WriteLine(dinosaur)
        Next

        Console.WriteLine(vbLf & "output = dinosaurs.GetRange(2, 3).ToArray")
        Dim output() As String = dinosaurs.GetRange(2, 3).ToArray()

        Console.WriteLine()
        For Each dinosaur As String In output
            Console.WriteLine(dinosaur)
        Next

    End Sub
End Class

' This code example produces the following output:
'
'Capacity: 3
'
'Brachiosaurus
'Amargasaurus
'Mamenchisaurus
'
'AddRange(dinosaurs)
'
'Brachiosaurus
'Amargasaurus
'Mamenchisaurus
'Brachiosaurus
'Amargasaurus
'Mamenchisaurus
'
'RemoveRange(2, 2)
'
'Brachiosaurus
'Amargasaurus
'Amargasaurus
'Mamenchisaurus
'
'InsertRange(3, input)
'
'Brachiosaurus
'Amargasaurus
'Amargasaurus
'Tyrannosaurus
'Deinonychus
'Velociraptor
'Mamenchisaurus
'
'output = dinosaurs.GetRange(2, 3).ToArray
'
'Amargasaurus
'Tyrannosaurus
'Deinonychus
using System;
using System.Collections.Generic;

public class Example
{
    public static void Main()
    {
        string[] input = { "Brachiosaurus",
                           "Amargasaurus",
                           "Mamenchisaurus" };

        List<string> dinosaurs = new List<string>(input);

        Console.WriteLine("\nCapacity: {0}", dinosaurs.Capacity);

        Console.WriteLine();
        foreach( string dinosaur in dinosaurs )
        {
            Console.WriteLine(dinosaur);
        }

        Console.WriteLine("\nAddRange(dinosaurs)");
        dinosaurs.AddRange(dinosaurs);

        Console.WriteLine();
        foreach( string dinosaur in dinosaurs )
        {
            Console.WriteLine(dinosaur);
        }

        Console.WriteLine("\nRemoveRange(2, 2)");
        dinosaurs.RemoveRange(2, 2);

        Console.WriteLine();
        foreach( string dinosaur in dinosaurs )
        {
            Console.WriteLine(dinosaur);
        }

        input = new string[] { "Tyrannosaurus",
                               "Deinonychus",
                               "Velociraptor"};

        Console.WriteLine("\nInsertRange(3, input)");
        dinosaurs.InsertRange(3, input);

        Console.WriteLine();
        foreach( string dinosaur in dinosaurs )
        {
            Console.WriteLine(dinosaur);
        }

        Console.WriteLine("\noutput = dinosaurs.GetRange(2, 3).ToArray()");
        string[] output = dinosaurs.GetRange(2, 3).ToArray();

        Console.WriteLine();
        foreach( string dinosaur in output )
        {
            Console.WriteLine(dinosaur);
        }
    }
}

/* This code example produces the following output:

Capacity: 3

Brachiosaurus
Amargasaurus
Mamenchisaurus

AddRange(dinosaurs)

Brachiosaurus
Amargasaurus
Mamenchisaurus
Brachiosaurus
Amargasaurus
Mamenchisaurus

RemoveRange(2, 2)

Brachiosaurus
Amargasaurus
Amargasaurus
Mamenchisaurus

InsertRange(3, input)

Brachiosaurus
Amargasaurus
Amargasaurus
Tyrannosaurus
Deinonychus
Velociraptor
Mamenchisaurus

output = dinosaurs.GetRange(2, 3).ToArray()

Amargasaurus
Tyrannosaurus
Deinonychus
 */
Visual C++
using namespace System;
using namespace System::Collections::Generic;

void main()
{
    array<String^>^ input = { "Brachiosaurus",
                              "Amargasaurus",
                              "Mamenchisaurus" };

    List<String^>^ dinosaurs =
        gcnew List<String^>((IEnumerable<String^>^) input);

    Console::WriteLine("\nCapacity: {0}", dinosaurs->Capacity);

    Console::WriteLine();
    for each(String^ dinosaur in dinosaurs )
    {
        Console::WriteLine(dinosaur);
    }

    Console::WriteLine("\nAddRange(dinosaurs)");
    dinosaurs->AddRange(dinosaurs);

    Console::WriteLine();
    for each(String^ dinosaur in dinosaurs )
    {
        Console::WriteLine(dinosaur);
    }

    Console::WriteLine("\nRemoveRange(2, 2)");
    dinosaurs->RemoveRange(2, 2);

    Console::WriteLine();
    for each(String^ dinosaur in dinosaurs )
    {
        Console::WriteLine(dinosaur);
    }

    input = gcnew array<String^> { "Tyrannosaurus",
                                   "Deinonychus",
                                   "Velociraptor"};

    Console::WriteLine("\nInsertRange(3, (IEnumerable<String^>^) input)");
    dinosaurs->InsertRange(3, (IEnumerable<String^>^) input);

    Console::WriteLine();
    for each(String^ dinosaur in dinosaurs )
    {
        Console::WriteLine(dinosaur);
    }

    Console::WriteLine("\noutput = dinosaurs->GetRange(2, 3)->ToArray()");
    array<String^>^ output = dinosaurs->GetRange(2, 3)->ToArray();

    Console::WriteLine();
    for each(String^ dinosaur in output )
    {
        Console::WriteLine(dinosaur);
    }
}

/* This code example produces the following output:

Capacity: 3

Brachiosaurus
Amargasaurus
Mamenchisaurus

AddRange(dinosaurs)

Brachiosaurus
Amargasaurus
Mamenchisaurus
Brachiosaurus
Amargasaurus
Mamenchisaurus

RemoveRange(2, 2)

Brachiosaurus
Amargasaurus
Amargasaurus
Mamenchisaurus

InsertRange(3, (IEnumerable<String^>^) input)

Brachiosaurus
Amargasaurus
Amargasaurus
Tyrannosaurus
Deinonychus
Velociraptor
Mamenchisaurus

output = dinosaurs->GetRange(2, 3)->ToArray()

Amargasaurus
Tyrannosaurus
Deinonychus
 */
Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98, Windows CE, Windows Mobile for Smartphone, Windows Mobile for Pocket PC, Xbox 360

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5, 3.0, 2.0

.NET Compact Framework

Supported in: 3.5, 2.0

XNA Framework

Supported in: 2.0, 1.0

Yazı kategorisi: Array, Generic, List | » yorum bırak;

java.util

Yazan: esersahin 08/11/2008

http://www.exampledepot.com/egs/java.util/pkg.html?l=rel#Lists

java.util

e331. Generating a Random Number
e332. Breaking a String into Words
e333. Creating a Custom Event
e334. Implementing a Simple Event Notifier
e335. Listing All Available Locales
e336. Setting the Default Locale
e337. Associating a Value with an Object

Arrays


e338. Comparing Arrays
e1074. Filling Elements in an Array
e339. Shuffling the Elements of a List or Array
e340. Converting a Collection to an Array
e341. Converting an Array to a Collection

Collections


e342. Implementing a Queue
e343. Implementing a Stack
e344. Implementing a Least-Recently-Used (LRU) Cache
e345. Listing the Elements of a Collection
e346. Storing Primitive Types in a Collection
e347. Creating a Copy of a Collection
e348. Making a Collection Read-Only

Lists


e349. Creating a List
e350. Sorting a List
e351. Operating on Lists
e1075. Creating a Type-Specific List [5.0]

Sets


e352. Creating a Set
e353. Operating on Sets
e354. Creating a Set That Retains Order-of-Insertion

Hash Tables


e355. Creating a Hash Table
e356. Creating a Map That Retains Order-of-Insertion
e357. Automatically Removing an Unreferenced Element from a Hash Table
e1076. Creating a Type-Specific Map [5.0]

Sorted Collections


e358. Creating a Sorted Set
e359. Sorting an Array
e360. Finding an Element in a Sorted Array
e1077. Inserting an Element into a Sorted Array
e361. Finding an Element in a Sorted List
e362. Inserting an Element into a Sorted List

Bits


e363. Performing Bitwise Operations on a Bit Vector
e364. Converting Between a BitSet and a Byte Array

Property Files


e365. Reading and Writing a Properties File
e366. Getting and Setting Properties

Timers


e367. Scheduling a Timer Task to Run at a Certain Time
e368. Scheduling a Timer Task to Run Repeatedly

Time


e369. Getting the Current Time
e370. Getting the Current Time in Another Time Zone
e371. Retrieving Information on All Available Time Zones
e372. Converting Times Between Time Zones

Dates


e373. Getting the Current Date
e374. Creating a Date Object for a Particular Date
e375. Determining the Number of Days in a Month
e376. Comparing Dates
e377. Determining a Person’s Age
e378. Determining If a Year Is a Leap Year
e379. Determining the Day-of-Week for a Particular Date

Yazı kategorisi: Java, Java.Util | » yorum bırak;

Converting an Array to a Collection

Yazan: esersahin 08/11/2008

http://www.exampledepot.com/egs/java.util/coll_Array2Col.html?l=rel

// Fixed-size list
    List list = Arrays.asList(array);

    // Growable list
    list = new LinkedList(Arrays.asList(array));

    // Duplicate elements are discarded
    Set set = new HashSet(Arrays.asList(array));

Yazı kategorisi: Array, Java | » yorum bırak;

Comparing Arrays

Yazan: esersahin 08/11/2008

http://www.exampledepot.com/egs/java.util/coll_CompArray.html?l=rel

// null arrays are equal
    boolean[] bArr1 = null;
    boolean[] bArr2 = null;
    boolean b = Arrays.equals(bArr1, bArr2);                   // true

    // Compare two boolean arrays
    bArr1 = new boolean[]{true, false};
    bArr2 = new boolean[]{true, false};
    b = Arrays.equals(bArr1, null);                            // false
    b = Arrays.equals(bArr1, bArr2);                           // true

    // There are equals() methods for all eight primitive types
    b = Arrays.equals(new byte[]{0}, new byte[]{0});           // true
    b = Arrays.equals(new char[]{'a'}, new char[]{'a'});       // true
    b = Arrays.equals(new short[]{0}, new short[]{0});         // true
    b = Arrays.equals(new int[]{0}, new int[]{0});             // true
    b = Arrays.equals(new long[]{0L}, new long[]{0L});         // true
    b = Arrays.equals(new float[]{0F}, new float[]{0F});       // true
    b = Arrays.equals(new double[]{0D}, new double[]{0D});     // true

    // When comparing Object arrays, null elements are equal.
    // If the elements are not null, Object.equals() is used.
    b = Arrays.equals(new String[]{"a"}, new String[]{"a"});   // true
    b = Arrays.equals(new String[]{null}, new String[]{null}); // true
    b = Arrays.equals(new String[]{"a"}, new String[]{null});  // false

Yazı kategorisi: Array, Java | » yorum bırak;

The Java Developers Almanac 1.4

Yazan: esersahin 08/11/2008

http://www.exampledepot.com/

http://www.exampledepot.com/egs/java.util/pkg.html

Yazı kategorisi: Java | » yorum bırak;

Filling Elements in an Array

Yazan: esersahin 08/11/2008

http://www.exampledepot.com/egs/java.util/coll_FillArray.html?l=rel

The Arrays class has conveninent methods for filling arrays of all eight primitive types:

    boolean[] booleanArr = new boolean[10];
    boolean booleanFillValue = false;
    Arrays.fill(booleanArr, booleanFillValue);

    byte[] byteArr = new byte[10];
    byte byteFillValue = (byte)0xFF;
    Arrays.fill(byteArr, byteFillValue);

    char[] charArr = new char[10];
    char charFillValue = 0xFF;
    Arrays.fill(charArr, charFillValue);

    short[] shortArr = new short[10];
    short shortFillValue = 0xFF;
    Arrays.fill(shortArr, shortFillValue);

    int[] intArr = new int[10];
    int intFillValue = -1;
    Arrays.fill(intArr, intFillValue);

    long[] longArr = new long[10];
    long longFillValue = -1;
    Arrays.fill(longArr, longFillValue);

    float[] floatArr = new float[10];
    float floatFillValue = -1;
    Arrays.fill(floatArr, floatFillValue);

    double[] doubleArr = new double[10];
    double doubleFillValue = -1;
    Arrays.fill(doubleArr, doubleFillValue);

There is also a method for filling object arrays:

    String[] StringArr = new String[1];
    String StringFillValue = "";
    Arrays.fill(StringArr, StringFillValue);

By default, the nine fill methods shown above set all the elements in the array with the fill value. Each fill method has an overloaded version that can restrict the fill to a contiguous range of elements:

    // Fill elements 0, 1, 2, and 3; the end index is exclusive
    int startIndex = 0;
    int endIndex = 4;

    Arrays.fill(booleanArr, startIndex, endIndex, booleanFillValue);
    Arrays.fill(byteArr, startIndex, endIndex, byteFillValue);
    Arrays.fill(charArr, startIndex, endIndex, charFillValue);
    Arrays.fill(shortArr, startIndex, endIndex, shortFillValue);
    Arrays.fill(intArr, startIndex, endIndex, intFillValue);
    Arrays.fill(longArr, startIndex, endIndex, longFillValue);
    Arrays.fill(floatArr, startIndex, endIndex, floatFillValue);
    Arrays.fill(doubleArr, startIndex, endIndex, doubleFillValue);
    Arrays.fill(StringArr, startIndex, endIndex, StringFillValue);

Yazı kategorisi: Array, Java | » yorum bırak;

Converting a Collection to an Array from The Java Developers Almanac 1.4

Yazan: esersahin 08/11/2008

http://www.exampledepot.com/egs/java.util/coll_GetArrayFromVector.html

// Create an array containing the elements in a list
    Object[] objectArray = list.toArray();
    MyClass[] array = (MyClass[])list.toArray(new MyClass[list.size()]);

    // Create an array containing the elements in a set
    objectArray = set.toArray();
    array = (MyClass[])set.toArray(new MyClass[set.size()]);

    // Create an array containing the keys in a map
    objectArray = map.keySet().toArray();
    array = (MyClass[])map.keySet().toArray(new MyClass[set.size()]);

    // Create an array containing the values in a map
    objectArray = map.values().toArray();
    array = (MyClass[])map.values().toArray(new MyClass[set.size()]);

Yazı kategorisi: Array, Java | » yorum bırak;