using System;
namespace Hello_World
{
public class EmptyClass
{
static void Main(string[] args)
{
//declare jaggedAraay
int[][] jaggedArray = new int[3][];
jaggedArray[0] = new int[5];
jaggedArray[1] = new int[3];
jaggedArray[2] = new int[2];
jaggedArray[0] = new int[] { 2, 3, 5, 7, 11 };
jaggedArray[1] = new int[] { 1, 2, 3 };
jaggedArray[2] = new int[] { 13, 21 };
//alternative way
int[][] jaggedArray2 = new int[][]
{
new int[]{ 2, 3, 5, 7, 11 },
new int[] { 1, 2, 3 }
};
Console.WriteLine("The Value in the middle of the first entry is {0}", jaggedArray2[0][2]);
for (int i = 0; i < jaggedArray2.Length; i++)
{
Console.WriteLine("Element {0}", i);
for (int j = 0; j < jaggedArray2[i].Length; j++)
Console.WriteLine("{0} ", jaggedArray2[i][j]);
}
Console.ReadKey();
}
}
}
가변길이배열 이용해서 가족 소개하기
using System;
namespace Hello_World
{
public class EmptyClass
{
static void Main(string[] args)
{
string[] joesFamily = new string[] { "Martha", "Robert" };
string[][] friendsAndFamily = new string[][]
{
new string[]{"michael", "Sandy"},
new string[]{"Frank", "Claudia"},
new string[]{"Andrew", "Michelle"},
joesFamily
};
Console.WriteLine("Hi {0}, I would like to introduce {1} to you.", friendsAndFamily[0][0], friendsAndFamily[1][0]);
Console.WriteLine("Hi {0}, I would like to introduce {1} to you.", friendsAndFamily[0][1], friendsAndFamily[2][0]);
Console.WriteLine("Hi {0}, I would like to introduce {1} to you.", friendsAndFamily[0][1], friendsAndFamily[2][1]);
Console.WriteLine("Hi {0}, I would like to introduce {1} to you.", friendsAndFamily[3][1], friendsAndFamily[2][1]);
Console.ReadKey();
}
}
}
using System;
namespace Hello_World
{
public class EmptyClass
{
static void Main(string[] args)
{
int price = 50;
float pi = 3.14f;
char at = '@';
string book = "The Hobbit";
ParamsMethod2(price, pi, at, book);
ParamsMethod2("Hello", 5.13, '$');
ParamsMethod("this", "is", "a", "long", "string", "...");
}
public static void ParamsMethod(params string[] sentence)
{
for (int i = 0; i < sentence.Length; i++)
{
Console.Write(sentence[i] + " ");
}
}
public static void ParamsMethod2(params object[] stuff) //
{
foreach (object obj in stuff)
{
Console.Write(obj + " ");
}
}
}
}
배열로 foreach 이용해서 최소값 구하기
using System;
namespace Hello_World
{
public class EmptyClass
{
static void Main(string[] args)
{
int min = MinV2(6, 4, 3, 8, -1, 1, 5);
Console.WriteLine("The minimum is : {0} ", min);
}
public static int MinV2(params int[] numbers)
{
int min = int.MaxValue;
foreach (int number in numbers)
{
if (number < min)
min = number;
}
return min;
}
}
}
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Hello_World
{
public class EmptyClass
{
static void Main(string[] args)
{
//declaring an ArrayList with undefined amount of object
ArrayList myArrayList = new ArrayList();
//declaring an ArrayList with undefined amount of object
ArrayList myArrayList2 = new ArrayList(100); //100 means "how many 'objects' will be in there"
myArrayList.Add(25);
myArrayList.Add("Hello");
myArrayList.Add(13.37);
myArrayList.Add(13);
myArrayList.Add(128);
myArrayList.Add(25.3);
myArrayList.Add(13);
//delete element with specific value from the arraylist
myArrayList.Remove(13); //you need to define what you want to remove
myArrayList.Remove(13); //you need to define what you want to remove
myArrayList.Remove(13); //you need to define what you want to remove
//delete element at specific position
myArrayList.RemoveAt(0);
Console.WriteLine(myArrayList.Count);
double sum = 0;
foreach (object obj in myArrayList)
{
if(obj is int)
{
sum += Convert.ToDouble(obj);
}else if(obj is double)
{
sum += (double)obj;
}
else if(obj is string)
{
Console.WriteLine(obj);
}
}
Console.WriteLine(sum);
Console.ReadKey();
}
}
}
'c#(유데미)' 카테고리의 다른 글
뒤끝 GameData 게임정보 CRUD 하는법 튜토리얼 (0) | 2023.10.25 |
---|---|
C#:객체 Car클래스 메서드 이용하기/배열arrays(3D차원) (0) | 2023.10.03 |
c#:do while 구문/loops/평균점수값 구하기 (0) | 2023.10.03 |
c#기초: 로그인,회원가입/최고점수 누군지 코드만들기 (0) | 2023.09.29 |
C#:user input (0) | 2023.09.27 |