五言語 基礎文法比較(C#,Java,Python,Ruby,Kotlin)

五言語 基礎文法比較
(C#,Java,Python,Ruby,Kotlin)

目次

コンソール出力

C# 
//Systemは省略可能
System.Console.Write("Hello ");
Console.Write("World");
>>> Hello World

Console.WriteLine("Hello ");
Console.WriteLine("Workd");
>>> Hello
    World


Java
System.out.print("Hello ");
System.out.print("World");
>>> Hello World

System.out.println("Hello ");
System.out.println("World");
>>> Hello
    World


Python
print("Hello")
print("World")
>>> Hello
    World

print("Hello ", end="")
print("World")

>>> Hello World


Ruby
print 'Hello '
print 'World'
>>> Hello World

puts 'Hello'
puts 'World'

>>> Hello 
    World


Kotlin
print("Hello ");
print("World");
>>> Hello World

println("Hello ");
println("Workd");
>>> Hello
    World



変数宣言

C# 
String stra = "a";
string strb = "b";
int num = 1;
Boolean tr = true;
bool fa = false;


Java
String str = "a";
int num = 1
boolean bool = true


Python

型の宣言は不要

str = "a" #String
num = 1 #int
tr = True #Boolean


Ruby

型の宣言は不要

Boolean型は存在しません

str = "a" #String
num = 1 #int


Kotlin
val str = "a" //String
//valで宣言した変数は変更不可

var num = 1 //int
//varで宣言した変数は変更可能
    num = 2

var tr = True //Boolean



条件分岐

C# 
///if
if(条件A){
    処理A
}
else if(条件B){
    処理B
}
else{
//上記の条件に当てはまらない場合
    処理C
}


//switch
switch (変数){
    case 条件A:
            処理A
            break;

    case 条件B:
            処理B
            break;

    default:
            //上記の条件に当てはまらない場合
            処理C
            break;
  }


Java
//if
if(条件A){
    処理A
}
else if(条件B){
    処理B
}
else{
    //上記の条件に当てはまらない場合
    処理C
}


//switch
switch (変数){
    case 条件A:
            処理A
            break;

    case 条件B:
            処理B
            break;

    default:
            //上記の条件に当てはまらない場合
            処理C
            break;
  }


Python
if   条件A:
    処理A

elif 条件B:
    処理B

else:
    処理C

※Pythonにはswitch文はありません


Ruby
#if
if   条件A then
    処理A

elsif 条件B then
    処理B

else then
    //上記の条件に当てはまらない場合
    処理C
end


#case
case 変数
when 条件A then
  処理A
when 条件B then
  処理B
else
  //上記の条件に当てはまらない場合
  処理C

end


Kotlin
//if
if(条件A){
    処理A
}
else if(条件B){
    処理B
}
else{
    //上記の条件に当てはまらない場合
    処理C
}

//when
when (変数) {
    条件A -> {
        処理A
    }
    条件B -> {
        処理B
    }
    else -> {
        処理C
    }

//whenは直接 変数==条件 等の式を入れても判定できます
when {
    条件式A -> {
        処理A
    }
    条件式B -> {
        処理B
    }
    else -> {
        処理C
    }
}



繰り返し処理

例1:1から100までの数字を出力

C# 
for (int i = 1; i <= 100; i++){
    Console.WriteLine(i);
}


Java
for (int i = 1; i <= 100; i++){
    System.out.println(i);
}


Python
for i in range(1, 101):
    print(i)        


Ruby
#3種類
puts [*1..100]

100.times{|n| println n+1}

1.upto(100){|n| p n}


Kotlin
for (i in 1..100) println(i)



例2:配列の中身をすべて出力

C# 
int[] array = new int[10]{1, 2, 4, 8, 16, 32, 64, 128, 256, 512};

foreach(int n in array)
{
  Console.WriteLine(n);
}


Java
int[] array = {1, 2, 4, 8, 16, 32, 64, 128, 256, 512};

for (int n : array) {
    System.out.println(n);
}


Python
array = [1, 2, 4, 8, 16, 32, 64, 128, 256, 512];

for n in array:
    print(n)   
Ruby
array = Array[1, 2, 4, 8, 16, 32, 64, 128, 256, 512];

for n in array do
    puts(n)   
end


Kotlin
val arr = arrayOf(1, 2, 4, 8, 16, 32, 64, 128, 256, 512)
for (n in arr) {
  println(n)
}



コレクション

配列

C# 
int[] ary = {1,10,100};


Java
int[] ary = {1,10,100};


Python
#Pythonではarrayはインポートしないと使えない
import array

ary = array.array('i',[1, 10, 100]) 
        #array.array(型,[値1,値2,値3])   


Ruby
array = [1,10,100]
//Rubyの配列は変更可能
array.push(200)
array[2] = 50

p array
>> [1,10,50,200]


Kotlin
// [1, 2, 3]を作る
val list = arrayOf(1, 2, 3)

// nullで埋められたサイズ3の配列を作る
var arr: Array<Int?> = arrayOfNulls(3)



リスト

C# 
//宣言
 var list = new List<string>();
//var オブジェクト名 = new List<型>(要素数);

//追加
      list.Add("Tokyo");
      list.Add("Osaka");
      list.Add("Nagoya");

//削除      
      list.Remove("Osaka");

//出力      
      Console.WriteLine("[{0}]", string.Join(", ", list));
      Console.WriteLine(list[1]);

//出力結果
>>> [Tokyo, Nagoya]
    Nagoya


Java
import java.util.ArrayList;
import java.util.List;

//宣言
List<String> list = new ArrayList<String>();
//List<リストに入れる物の型> オブジェクト名 = new Listの型<リストに入れる物の型>(要素数)

//追加
    list.add("Tokyo");
    list.add("Osaka");
    list.add("Nagoya");
//削除
    list.remove("Osaka");
//出力
    System.out.println(list);
    System.out.println(list.get(1));

//出力結果
>>> [Tokyo, Nagoya]
    Nagoya


Python
list = ["Hello", 1, "world", 2, "!"]

#追加
list.append(5)

#削除
list.remove(1)

#出力
print(list)
print(list[3])

#出力結果
>>> ['Hello','world', 2, '!',5]
    !


Ruby

RubyはArrayがListと同じ役割をするためListはありません

Kotlin
// List<Int> を作る。Listは中身を変更できない。
val list = listOf(1, 2, 3, 4, 5)

// MutableList<Int> を作る。こっちは中身を変更可能
val mlist = mutableListOf(1, 2, 3, 4, 5)

//追加
mlist.add(6)

//削除
mlist.remove(3)

//出力
println(mlist[0])
println(mlist)
>>> 1
    [1, 2, 4, 5,6]



キーによる管理

C# 
Dictionary<int,String> dict = new Dictionary<int,String>();
//Dictionary<Keyの型名, Valueの型名> オブジェクト名 = new Dictionary<Keyの型名, Valueの型名>()

//追加
dict.Add(1,"aaa");
dict.Add(2,"bbb");
dict.Add(3"ccc");

//削除
dict.Remove(2)

//出力
Console.WriteLine(dict[3])
>>> ccc

//全件出力
foreach (KeyValuePair<int, string> item in dict){
    Console.WriteLine("[{0}:{1}]", item.Key, item.Value);
}
>>> [1:aaa]    
    [2:bbb]


Java
import java.util.HashMap;
import java.util.Map;

//宣言
Map<Integer,String> map = new HashMap<Integer,String>();
//Map<Keyの型名, Valueの型名> オブジェクト名 = new Mapの型<Keyの型名, Valueの型名>()

//追加
    map.put(1,"aaa");
    map.put(2,"bbb");
    map.put(3,"ccc");

//削除
    map.remove(2);

//出力
    System.out.println(map.get(3));
>> ccc

//全件出力
    for (int key : map.keySet()) {
        System.out.println(key + ":" + map.get(key));
    }
>>> 1:aaa
    3:ccc


Python
#宣言(初期化)
dict = {1:"aaa", 2:"bbb", 3:"ccc", "ddd":4}
#オブジェクト名 = {key:value , key;value}

#追加
dict["peach"] = 5

#削除
dict.pop(2)

#出力
print(dict[3])
print(dict["ddd"])
>>> ccc
    4

#全件出力
print(dict)
>>> {1: 'aaa', 3: 'ccc', 'ddd': 4, 'peach': 5}


Ruby
#Hashオブジェクトを使う

hash = {1 =>"aaa", 2 =>"bbb", 3 => "ccc", "ddd" => 4}
#オブジェクト名 = { key => value}

#追加
hash[5] = "eee"

#削除
hash.delete[2]

#出力
puts hash["ddd"]
>>>4

#全件出力
puts hash.inspect
>>> {:1=>aaa, :3=>cccc :ddd=>4 :5=>eee}


Kotlin
//Map<Int,String>を作る、変更できない
val map = mapOf(1 to "aaa", 2 to "bbb")
// val オブジェクト名 = mapOf(key to value)

// MutableMap<Int, String> を作る、変更できる
val mmap = mutableMapOf(1 to "aaa", 2 to "bbb")
mmap[1] = "ccc" 

//追加
mmap[3] = "ddd"

//削除
mmap.remove(2)

//出力
println(list[1])
>>> ccc

//全件出力
for((key, value) in map)
    println("${key} to ${value}")
>>> 1 to ccc
    3 to ddd





Why do not you register as a user and use Qiita more conveniently?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
Sign up for free and join this conversation.
If you already have a Qiita account