#はじめに#
JavaとPythonの基礎文法を比較してみました。
Javaの文法が汚かったり、違う部分があればご指摘いただけるとありがたいです!
#目次#
1.出力
2.変数
3.文字列と数値の結合
4.if文
5.for文
6.while文
7.forとwhileでbreak, continue
8.list
9.辞書
#1.出力#
print('123')
print(123)
>>>123
>>>123
public class Main {
public static void main(String[] args){
System.out.println("123");
System.out.println(123);
}
}
>>>123
>>>123
#2.変数#
mystr = 'hello'
myint = 123
print(mystr)
print(myint)
>>>hello
>>>123
public class Main {
public static void main(String[] args){
String mystr = "hello";
int myint = 123;
System.out.println(mystr);
System.out.println(myint);
}
}
>>>hello
>>>123
#3.文字列と数値の結合#
myint = 123
print('数字は' + str(myint))
>>>数字は123
public class Main {
public static void main(String[] args){
int myint = 123;
System.out.println("数字は" + myint);
}
}
>>>数字は123
#4.if文#
myint = 10
if myint == 10:
print('myintは10です')
>>>myintは10です
public class Main {
public static void main(String[] args){
int myint = 10;
if(myint == 10){
System.out.println("myintは10です");
}
}
}
>>>myintは10です
#5.for文#
for char in 'Hello':
print(char)
>>>H
>>>e
>>>l
>>>l
>>>o
//splitと拡張for文
public class Main {
public static void main(String[] args){
String mystr = "hello";
String[] strArray = mystr.split("");
for(String s : strArray) {
System.out.println(s);
}
}
}
//charAtとfor文
public class Main {
public static void main(String[] args){
String mystr = "hello";
for(int i = 0; i < mystr.length(); i++) {
System.out.println(mystr.charAt(i));
}
}
}
>>>h
>>>e
>>>l
>>>l
>>>o
###・指定回数だけループ###
for i in range(3):
print(i)
>>>0
>>>1
>>>2
public class Main {
public static void main(String[] args){
for(int i = 0; i < 3; i++) {
System.out.println(i);
}
}
}
>>>0
>>>1
>>>2
###・リスト、配列のループ処理###
list = ['item1', 'item2', 'item3']
for item in list:
print(item)
>>>item1
>>>item2
>>>item3
public class Main {
public static void main(String[] args){
String[] list = {"item1", "item2", "item3"};
for(int i = 0; i <list.length; i++){
System.out.println(list[i]);
}
}
}
>>>item1
>>>item2
>>>item3
#6.While文#
val = 0
while val < 4:
val += 1
print(val)
print('valは' + str(val))
>>>1
>>>2
>>>3
>>>4
>>>valは4
public class Main {
public static void main(String[] args){
int val = 0;
while(val < 4){
val++;
System.out.println(val);
}
System.out.println("valは" + val);
}
}
>>>1
>>>2
>>>3
>>>4
>>>valは4
#7.forとwhileでbreak, continue
###・for文でbreak###
list = [1,2,3,4,5]
for i in list:
if i == 3:
break
print(i)
>>>1
>>>2
>>>breakで繰り返し処理終了
public class Main {
public static void main(String[] args){
int[] list = {1,2,3,4,5};
for(int i = 1; i < list.length; i++){
if(i == 3){
break;
}
System.out.println(i);
}
}
}
>>>1
>>>2
>>>breakで繰り返し処理終了
###・while文でbreak###
val = 1
while val < 10:
if val == 3:
break
print(val)
val += 1
>>>1
>>>2
>>>breakで繰り返し処理終了
public class Main {
public static void main(String[] args){
int val = 1;
while(val < 10){
if(val == 3){
break;
}
System.out.println(val);
val++;
}
}
}
>>>1
>>>2
>>>breakで繰り返し処理終了
###・for文でcontinue###
# for文でcontinue
list = [1,2,3,4,5]
for i in list:
if i == 2:
continue
print(i)
>>>1
>>>3
>>>4
>>>5
public class Main {
public static void main(String[] args){
int[] list = {1,2,3,4,5};
for(int i = 1; i < list.length+1; i++){
if(i == 2){
continue;
}
System.out.println(i);
}
}
}
>>>1
>>>3
>>>4
>>>5
###・while文でcontinue###
val = 0
while val < 5:
val += 1
if val == 2:
continue
print(val)
>>>1
>>>3
>>>4
>>>5
public class Main {
public static void main(String[] args){
int val = 1;
while(val < 6){
if(val == 2){
val++;
continue;
}
System.out.println(val);
val++;
}
}
}
>>>1
>>>3
>>>4
>>>5
#8.list#
pythonではリストが配列の役割を担っています。
list = ['a','b','c']
print(list)
>>>['a','b','c']
Javaではリストの中身を全部一気に取得できないので、forを使って一つずつ呼び出す必要があります。
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class Main {
public static void main(String[] args){
List<String> list = new ArrayList<String>(Arrays.asList("a", "b", "c"));
for(int i = 0; i < list.size(); i++) {
System.out.println(list.get(i));
}
}
}
>>>a
>>>b
>>>c
###・リスト内の要素の参照###
list = ['a','b','c']
#先頭の要素
print(list[0])
#2番目の要素
print(list[2])
>>>a
>>>c
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class Main {
public static void main(String[] args){
List<String> list = new ArrayList<String>(Arrays.asList("a", "b", "c"));
//先頭の要素
System.out.println(list.get(0));
//2番目の要素
System.out.println(list.get(2));
}
}
>>>a
>>>c
###・リストへの値の追加###
list = ['a','b','c']
#末尾に追加
list.append('d')
#指定位置に追加
list.insert(2, 'd')
print(list)
>>>['a','b','d','c','d']
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class Main {
public static void main(String[] args){
List<String> list = new ArrayList<String>(Arrays.asList("a", "b", "c"));
//末尾に追加
list.add("d");
//指定位置に追加
list.add(2, "d");
}
}
>>>{"a","b","d","c","d"}
###・リストからの値の削除###
list = ['a','b','c']
#末尾の要素を削除
list.pop()
#指定位置の要素を削除
list.pop(1)
print(list)
>>>['a']
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class Main {
public static void main(String[] args){
List<String> list = new ArrayList<String>(Arrays.asList("a", "b", "c"));
//指定位置の要素を削除
list.remove(0);
}
}
>>>{"b","c"}
###・リストの値の更新###
list = ['a','b','c']
list[1] = 'x'
print(list)
>>>['a','x','c']
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class Main {
public static void main(String[] args){
List<String> list = new ArrayList<String>(Arrays.asList("a", "b", "c"));
list.set(1,"x");
}
}
>>>{"a","x","c"}
#9.辞書、Map#
dict = { 'apple':1, 'orange':2, 'banana':3 }
print(dict['apple'])
>>>1
import java.util.LinkedHashMap;
public class Main {
public static void main(String[] args){
LinkedHashMap<String, Integer> map = new LinkedHashMap<>() ;
map.put("apple", 1);
map.put("orange", 2);
map.put("banana", 3);
System.out.println(map);
System.out.println(map.get("apple"));
}
}
>>>{apple=1, orange=2, banana=3}
>>>1
順序を保持して入れたかったので今回はLinkedHashMapを使いました。
Mapの種類と特徴
LinkedHashMap:
キーを格納した順に保持。
TreeMap:
キーの昇順に保持。
HashMap:
順序を保持しない。
###・辞書、Mapの値の更新###
dict = { 'apple':1, 'orange':2, 'banana':3 }
dict['apple'] = 5
print(dict)
>>>{ 'apple':5, 'orange':2, 'banana':3 }
import java.util.LinkedHashMap;
public class Main {
public static void main(String[] args){
LinkedHashMap<String, Integer> map = new LinkedHashMap<>() ;
map.put("apple", 1);
map.put("orange", 2);
map.put("banana", 3);
map.put("apple",5);
System.out.println(map);
}
}
>>>{apple=5, orange=2, banana=3}
###・辞書、Mapの値の削除###
dict = { 'apple':1, 'orange':2, 'banana':3 }
del dict['apple']
print(dict)
>>>{'orange':2, 'banana':3 }
import java.util.LinkedHashMap;
public class Main {
public static void main(String[] args){
LinkedHashMap<String, Integer> map = new LinkedHashMap<>() ;
map.put("apple", 1);
map.put("orange", 2);
map.put("banana", 3);
map.remove("apple");
System.out.println(map);
}
}
>>>{orange=2, banana=3}
###・キーの存在確認###
dict = { 'apple':1, 'orange':2, 'banana':3 }
if 'apple' in dict:
print('存在します')
else:
print('存在しません')
>>>存在します
import java.util.LinkedHashMap;
public class Main {
public static void main(String[] args){
LinkedHashMap<String, Integer> map = new LinkedHashMap<>() ;
map.put("apple", 1);
map.put("orange", 2);
map.put("banana", 3);
if(map.containsKey("apple")){
System.out.println("存在します");
}else{
System.out.println("存在しません");
}
}
}
>>>存在します