Light Blue Pointer
본문 바로가기
Developing/개발일지

2023-10-19, Today I Learned

by Greedy 2023. 10. 19.

⭐파이썬 이차원 배열 선언,읽기

arr = [[None for j in range(cols)] for i in range(rows)]

for i in range(rows):
            for j in range(cols):
                arr[i][j] = None

📖 틈새 파이썬 공부📖

딕셔너리 선언

⭐딕셔너리 선언

thisdict = {
  "brand": "Ford",
  "model": "Mustang",
  "year": 1964
}

thisdict = dict(name = "John", age = 36, country = "Norway")

 

딕셔너리 수정

⭐ 딕셔너리 추가

car["color"] = "white"

⭐ 딕셔너리 변경

thisdict["year"] = 2018
thisdict.update({"year": 2020})

딕셔너리 읽기

⭐ 딕셔너리 읽기 (Accessing Items)

thisdict["brand"]
x = thisdict["model"]
x = thisdict.get("model")

⭐ 키,값 목록 읽기

x = thisdict.keys()
x = thisdict.values()
x = thisdict.items()#Get a list of the key:value pairs

⭐ 키값이 존재하는지 보기

if "model" in thisdict:
  print("Yes, 'model' is one of the keys in the thisdict dictionary")

⭐ 딕셔너리 길이

len(thisdict)

⭐ 딕셔너리 루프

for x in thisdict:
  print(x)#Print all key names in the dictionary, one by one:

for x in thisdict:
  print(thisdict[x])#Print all values in the dictionary, one by one:

for x in thisdict.values():
  print(x)#to return values of a dictionary:

for x in thisdict.keys():
  print(x)#to return the keys of a dictionary:

for x, y in thisdict.items():
  print(x, y)#Loop through both keys and values, by using the items() method:

⭐ 딕셔너리 삭제

thisdict.pop("model")
thisdict.popitem()#removes the last inserted item
del thisdict["model"]
del thisdict#delete the dictionary completely:

thisdict.clear()#empties the dictionary:

🚨range 인덱스에 주의하기

for i in range(players): 오늘 한 실수 range를 쓰고싶은데 len을 안 씀
for x in range(len(arrays)): 며칠 전에 한 실수 그냥 x로 요소를 받아오고 싶은데 인덱스만 받아옴

 

⭐인텔리제이 코드 정렬

ctrl+shift+alt+L → Run 버튼 누르기

 

⭐ 자바 배열 선언과 초기화

int[] intArray = new int[5];

 

⭐ static 변수는 class 밑 메인 함수 시작하기 전에 써줘야 한다

*static int* [] *burgerCount* = *new int*[5];

 

⭐ 자바 배열 길이

int length = array.length;

⭐ 자바 상속

public Class Child extends Parent{}

 

함수만 가지고 코드 짜다가 깨달았다

과제에서는 상속을 쓰라고 했다는걸…

⭐ Java ArrayList

import java.util.ArrayList; // import the ArrayList class

ArrayList<String> cars = new ArrayList<String>(); // Create an ArrayList object

⭐ Java ArrayList 선언

ArrayList<String> cars = new ArrayList<String>();

⭐ Java ArrayList  쓰기

cars.add("Volvo");//추가
cars.set(0, "Opel");//수정
cars.remove(0);//삭제
cars.clear();//모두삭제

⭐ Java ArrayList  읽기

cars.get(0);//원소 
cars.size();//크기

 Java ArrayList Loop

for (int i = 0; i < cars.size(); i++) {
      System.out.println(cars.get(i));
    }

⭐ Java ArrayList  forEach loop

    ArrayList<String> cars = new ArrayList<String>();
    cars.add("Volvo");
    cars.add("BMW");
    cars.add("Ford");
    cars.add("Mazda");
    for (String i : cars) {
      System.out.println(i);
    }

⭐ Java ArrayList  Sort

ArrayList<String> cars = new ArrayList<String>();
    cars.add("Volvo");
    cars.add("BMW");
    cars.add("Ford");
    cars.add("Mazda");
    Collections.sort(cars);  // Sort cars
    for (String i : cars) {
      System.out.println(i);
    }

⭐ 자바 부모클래스에서 자식클래스 메소드 사용하기

**((Child)** parent**).**childMethod**()**

⭐자바 클래스 생성자

puclic Classname(){}//기본생성자
 Classname(Parameter parameter)(
)

⭐ 자바 객체 타입 알아내기

instanceOf
// 생성은 자식 String이지만 대입될 때 Object 타입으로 변환
	Object o1 = "string";
		
	// o1 변수가 참조하는 객체의 타입이 Object 타입?
	System.out.println(o1 instanceof Object);  // true
		
	// o1 변수가 참조하는 객체의 타입이 String 타입?
	System.out.println(o1 instanceof String);  // true
		
	// 무관한 타입이라서 false 출력
	System.out.println(o1 instanceof Integer);  // false
	// Integer가 Object를 상속받기 때문에 이건 syntax error가 아님
	}

⭐ 생성자 상위클래스의 생성자 이용

super

public Good(String name, String desc,double price){
        super(name, desc);
        this.price = price;
    }

 

💡값이 1,2,3,4,5,6일때 각각 다른 부분이 실행되어야 하고

공통으로 실행되는 부분이 있을때

if else 한번만 하는법

while(true){
	if (burger == 1) {
               
    } else if (burger == 2) {
               
    } else if (burger == 3) {
              
    } else if (burger == 4) {
              
    } else if (burger == 5) {
             
    }else{
         continue;//burger가 1~6이 아니면 다음 while문으로 가서 아래 코드 스킵됨
    }
    
    burger가 1~6일때만 실행하고 싶은 코드
    
}

elseif들 밑에다 else면 그냥continue;로 while문 다음으로 넘겨버리는 코드 만들어서 해결함

 

'Developing > 개발일지' 카테고리의 다른 글

2023-10-23 Today I learned  (0) 2023.10.23
2023-10-20 Today I Learned  (1) 2023.10.20
2023-10-10, Today I Learned  (0) 2023.10.10
2023-10-07, Today I Learned  (0) 2023.10.07
2023-10-06, Today I Learned  (2) 2023.10.06