.NET/Listを配列に配列をListに変換する
をテンプレートにして作成
[
トップ
] [
新規
|
一覧
|
検索
|
最終更新
|
ヘルプ
]
開始行:
#navi(../)
* Listを配列に配列をListに変換する [#m12238bd]
List<T>をArray[T]に、その逆のArray[T]をList<T>に変換する...
#contents
* 関連サイト [#k2ebbdb0]
-[[Microsoft .NET | List<T>.ToArray メソッド>https://docs...
-[[Microsoft .NET | List<T> コンストラクター>https://docs...
#htmlinsert(windev-top.html)
* 動作確認環境 [#h6bcfdf0]
-Windows 10 ver.1909
-Visual Studio 2019
-.NET Core
-コンソールアプリケーション
* サンプルコードおよび実行結果 [#j719c3ff]
以下に C#, Visual Basic のサンプルコードと実行結果を記し...
** C# [#d921a3c8]
#ref(Program.cs)
using System;
using System.Collections.Generic;
class Program
{
static void Main(string[] args)
{
List<string> lst = new List<string>
{
"Windows",
"macOS",
"Linux"
};
Console.WriteLine("-- List<T> --");
foreach (string item in lst)
{
Console.WriteLine("List : {0}", item);
}
Console.WriteLine("-- List<T> -> Array[T] --");
string[] ary = lst.ToArray();
Console.WriteLine("Count : {0}", lst.Count);
for (int i = 1; i <= lst.Count; i++)
{
Console.WriteLine("Array({0}) : {1}", i - 1,...
}
Console.WriteLine("-- Array[T] -> List<T> --");
List<string> lst2 = new List<string>(ary);
foreach (string item in lst2)
{
Console.WriteLine("List : {0}", item);
}
}
}
** Visual Basic [#h6011132]
#ref(Program.vb)
Imports System
Module Program
Sub Main(args As String())
Dim lst As List(Of String) = New List(Of String)
lst.Add("Windows")
lst.Add("macOS")
lst.Add("Linux")
Console.WriteLine("-- List(Of T) --")
For Each item As String In lst
Console.WriteLine("List : {0}", item)
Next
Console.WriteLine("-- List(Of T) -> Array(T) --")
Dim ary As String() = lst.ToArray()
Console.WriteLine("Count : {0}", lst.Count)
For i As Integer = 1 To lst.Count
Console.WriteLine("Array({0}) : {1}", i - 1,...
Next
Console.WriteLine("-- Array(T) -> List(Of T) --")
Dim lst2 As List(Of String) = New List(Of String...
For Each item As String In lst2
Console.WriteLine("List : {0}", item)
Next
End Sub
End Module
** 実行結果 [#qbeedb07]
上記のサンプルコードを実行した時のキャプチャです。
#ref(run.png)
以上、Listから配列(Array)、ArrayからListに変換するサンプ...
#htmlinsert(windev-btm.html)
終了行:
#navi(../)
* Listを配列に配列をListに変換する [#m12238bd]
List<T>をArray[T]に、その逆のArray[T]をList<T>に変換する...
#contents
* 関連サイト [#k2ebbdb0]
-[[Microsoft .NET | List<T>.ToArray メソッド>https://docs...
-[[Microsoft .NET | List<T> コンストラクター>https://docs...
#htmlinsert(windev-top.html)
* 動作確認環境 [#h6bcfdf0]
-Windows 10 ver.1909
-Visual Studio 2019
-.NET Core
-コンソールアプリケーション
* サンプルコードおよび実行結果 [#j719c3ff]
以下に C#, Visual Basic のサンプルコードと実行結果を記し...
** C# [#d921a3c8]
#ref(Program.cs)
using System;
using System.Collections.Generic;
class Program
{
static void Main(string[] args)
{
List<string> lst = new List<string>
{
"Windows",
"macOS",
"Linux"
};
Console.WriteLine("-- List<T> --");
foreach (string item in lst)
{
Console.WriteLine("List : {0}", item);
}
Console.WriteLine("-- List<T> -> Array[T] --");
string[] ary = lst.ToArray();
Console.WriteLine("Count : {0}", lst.Count);
for (int i = 1; i <= lst.Count; i++)
{
Console.WriteLine("Array({0}) : {1}", i - 1,...
}
Console.WriteLine("-- Array[T] -> List<T> --");
List<string> lst2 = new List<string>(ary);
foreach (string item in lst2)
{
Console.WriteLine("List : {0}", item);
}
}
}
** Visual Basic [#h6011132]
#ref(Program.vb)
Imports System
Module Program
Sub Main(args As String())
Dim lst As List(Of String) = New List(Of String)
lst.Add("Windows")
lst.Add("macOS")
lst.Add("Linux")
Console.WriteLine("-- List(Of T) --")
For Each item As String In lst
Console.WriteLine("List : {0}", item)
Next
Console.WriteLine("-- List(Of T) -> Array(T) --")
Dim ary As String() = lst.ToArray()
Console.WriteLine("Count : {0}", lst.Count)
For i As Integer = 1 To lst.Count
Console.WriteLine("Array({0}) : {1}", i - 1,...
Next
Console.WriteLine("-- Array(T) -> List(Of T) --")
Dim lst2 As List(Of String) = New List(Of String...
For Each item As String In lst2
Console.WriteLine("List : {0}", item)
Next
End Sub
End Module
** 実行結果 [#qbeedb07]
上記のサンプルコードを実行した時のキャプチャです。
#ref(run.png)
以上、Listから配列(Array)、ArrayからListに変換するサンプ...
#htmlinsert(windev-btm.html)
ページ名: