new는 객체를 만드는 연산자이며, Wow( ) 생성자 메서드를 사용하여 객체를 생성하는 것 입니다.
일반 메서드
public class Wow {
// 입력 X, 리턴 X
public void hello() {
System.out.println("hello");
}
// 입력 O, 리턴 X
public void print(String str) {
System.out.println(str);
}
// 입력 X, 리턴 O
public int year(){
return 365;
}
// 입력 O, 리턴 O
public int add(int x, int y) {
return x+y;
}
}
import java.util.ArrayList;
public class Array {
public static void main(String[] args) {
// 동적 배열 선언
ArrayList<String> str = new ArrayList<>();
}
}
ArrayList를 사용하기 위해 java.util.ArrayList를 import합니다. Java.util.*로 대체해도 가능합니다.
ArrayList<자료형> 배열이름 = new ArrayList<자료형 생략가능>(숫자쓰면 크기지정 / 리스트 쓰면 복사);
특별한 일이 없다면 ArrayList<String> str = new ArrayList<>();로 선언하셔도 좋습니다.
- ArrayList 값 추가
// 리스트에 값 추가
str.add("fjdkslvn@gmail.com");
str.add("djmax@gmail.com");
str.add("fullmetal@gmail.com");
System.out.println(str+" <-- 리스트에 값 추가");
// 원하는 위치에 값 추가
str.add(1,"demon@gmail.com");
System.out.println(str+" <-- 원하는 위치에 값 추가");
// 해당 위치 값 변경
str.set(2, "alchemist@gmail.com");
리스트명.add(내용물); 로 리스트에 값을 추가할 수 있습니다.
리스트명.add(위치,내용물); 로 위치를 지정해서 값을 추가할 수도 있습니다.
지정하지않으면 출력기준 맨 오른쪽에 들어갑니다.
리스트명.set(위치,내용물); 로 값을 변경할 수 있습니다.
- ArrayList 값 접근 / 문자열 자르기
// 특정 요소 접근
System.out.println(str.get(1)+" <-- 특정 요소 접근");
// 특정 요소 문자열 자르기
System.out.println(str.get(1).split("@")[0]+" <-- 특정 요소 문자열 자르기");
리스트명.get(위치)를 통해 특정 요소에 접근할 수 있습니다.
리스트명.get(위치).split("문자열을 자를 기준")[자른 후 선택할 문자열]을 통해 문자열을 자르고 원하는 값만 가져올 수 있습니다. 만약 위 코드에서 str.get(1).split("@")[1]이라고 했다면 gmail.com이 출력됐을겁니다.
- ArrayList 반복문을 통해 출력
// foreach를 사용하여 리스트 값 출력하기
for (String st : str) {
System.out.println(st);
}
* 일반적인 for문을 사용해도 무방합니다*
for(자료형 변수 : 리스트명)의 형태로 이루어져있습니다. 코드에서 st라는 부분은 각 리스트값을 가져올 변수를 의미합니다. 리스트 길이만큼 반복문이 진행되면서 리스트 요소를 st로 뽑아옵니다.
이걸 사용하면 숫자값이 1씩 증가하는 i변수를 쓰진 못하지만 간편합니다. 1씩 증가하는 숫자값을 쓰고싶다면 while에서 처럼 직접 변수를 생성하여 반복문이 끝날쯤에 숫자를 1씩 증가시켜줍시다.
- ArrayList 요소 포함 여부
// 특정 요소가 포함되어있는지 확인
boolean bool = str.contains("fjdkslvn@gmail.com");
System.out.println(bool+" <-- 특정 요소가 포함되어있는지");
// 특정 요소가 포함되어있으면 위치 추출
int num = str.indexOf("fjdkslvn@gmail.com");
System.out.println(num+" <-- 요소가 있으면 위치 추출");
리스트명.contains(요소)를 통해 특정 요소가 포함되어있는지 확인할 수 있습니다.
리스트명.index0f(요소)를 통해 특정 요소가 포함되어있으면 해당 위치를 추출할 수 있습니다.
=> 만약 포함되어있지 않다면 '-1'을 반환합니다.
- ArrayList 삭제 / 길이 추출
// 리스트 길이 추출
System.out.println(str.size()+" <-- 리스트 길이 추출");
// 리스트 요소 삭제
String rm = str.remove(1);
System.out.println(rm+" <-- 삭제된 값");
System.out.println(str+" <-- 리스트 요소 삭제 후");
// 리스트 비우기
str.clear();
System.out.println(str+" <-- 리스트 비우기");
리스트명.size()를 통해 리스트 길이를 추출할 수 있습니다.
리스트명.remove(위치)를 통해 리스트 요소를 삭제할 수 있고, 해당 값을 담아서 활용할 수 있습니다.
리스트명.clear()를 통해 리스트를 비울 수 있습니다.
전체 코드
Array.java
import java.util.ArrayList;
public class Array {
public static void main(String[] args) {
// 동적 배열 선언
ArrayList<String> str = new ArrayList<>();
// 리스트에 값 추가
str.add("fjdkslvn@gmail.com");
str.add("djmax@gmail.com");
str.add("fullmetal@gmail.com");
System.out.println(str+" <-- 리스트에 값 추가");
// 원하는 위치에 값 추가
str.add(1,"demon@gmail.com");
System.out.println(str+" <-- 원하는 위치에 값 추가");
// 해당 위치 값 변경
str.set(2, "alchemist@gmail.com");
System.out.println(str+" <-- 해당 위치 값 변경");
// 특정 요소 접근
System.out.println(str.get(1)+" <-- 특정 요소 접근");
// 특정 요소 문자열 자르기
System.out.println(str.get(1).split("@")[0]+" <-- 특정 요소 문자열 자르기");
// foreach를 사용하여 리스트 값 출력
for (String st : str) {
System.out.println(st);
}
// 특정 요소가 포함되어있는지 확인
boolean bool = str.contains("fjdkslvn@gmail.com");
System.out.println(bool+" <-- 특정 요소가 포함되어있는지");
// 특정 요소가 포함되어있으면 위치 추출
int num = str.indexOf("fjdkslvn@gmail.com");
System.out.println(num+" <-- 요소가 있으면 위치 추출");
// 리스트 길이 추출
System.out.println(str.size()+" <-- 리스트 길이 추출");
// 리스트 요소 삭제
String rm = str.remove(1);
System.out.println(rm+" <-- 삭제된 값");
System.out.println(str+" <-- 리스트 요소 삭제 후");
// 리스트 비우기
str.clear();
System.out.println(str+" <-- 리스트 비우기");
}
}
import java.util.Calendar;
public class Cal {
public static void main(String[] args) {
Calendar cal = Calendar.getInstance();
int year = cal.get(Calendar.YEAR);
int month = cal.get(Calendar.MONTH) + 1;
int day = cal.get(Calendar.DAY_OF_MONTH);
System.out.println(year+"-"+month+"-"+day);
}
}
public class test {
public static void main(String[] args) {
String str ="200";
int num;
// 문자를 숫자로 변환
num=Integer.parseInt(str);
System.out.println(num+" <--문자를 숫자로 변환");
num +=100; // num==300
// 숫자를 문자로 변환
str = Integer.toString(num);
System.out.println(str+" <--숫자를 문자로 변환");
}
}
Traceback (most recent call last): File "C:\Users\fjdks\AppData\Local\Programs\Python\Python39\lib\site-packages\pyzbar\zbar_library.py", line 58, in load dependencies, libzbar = load_objects(Path('')) File "C:\Users\fjdks\AppData\Local\Programs\Python\Python39\lib\site-packages\pyzbar\zbar_library.py", line 50, in load_objects deps = [ File "C:\Users\fjdks\AppData\Local\Programs\Python\Python39\lib\site-packages\pyzbar\zbar_library.py", line 51, in <listcomp> cdll.LoadLibrary(str(directory.joinpath(dep))) File "C:\Users\fjdks\AppData\Local\Programs\Python\Python39\lib\ctypes\__init__.py", line 452, in LoadLibrary return self._dlltype(name) File "C:\Users\fjdks\AppData\Local\Programs\Python\Python39\lib\ctypes\__init__.py", line 374, in __init__ self._handle = _dlopen(self._name, mode) FileNotFoundError: Could not find module 'libiconv.dll' (or one of its dependencies). Try using the full path with constructor syntax.
During handling of the above exception, another exception occurred:
Traceback (most recent call last): File "D:\한이음\QR\aws rds와 바코드인식 연동.py", line 3, in <module> from pyzbar import pyzbar File "C:\Users\fjdks\AppData\Local\Programs\Python\Python39\lib\site-packages\pyzbar\pyzbar.py", line 7, in <module> from .wrapper import ( File "C:\Users\fjdks\AppData\Local\Programs\Python\Python39\lib\site-packages\pyzbar\wrapper.py", line 139, in <module> zbar_version = zbar_function( File "C:\Users\fjdks\AppData\Local\Programs\Python\Python39\lib\site-packages\pyzbar\wrapper.py", line 136, in zbar_function return prototype((fname, load_libzbar())) File "C:\Users\fjdks\AppData\Local\Programs\Python\Python39\lib\site-packages\pyzbar\wrapper.py", line 115, in load_libzbar libzbar, dependencies = zbar_library.load() File "C:\Users\fjdks\AppData\Local\Programs\Python\Python39\lib\site-packages\pyzbar\zbar_library.py", line 60, in load dependencies, libzbar = load_objects(Path(__file__).parent) File "C:\Users\fjdks\AppData\Local\Programs\Python\Python39\lib\site-packages\pyzbar\zbar_library.py", line 54, in load_objects libzbar = cdll.LoadLibrary(str(directory.joinpath(fname))) File "C:\Users\fjdks\AppData\Local\Programs\Python\Python39\lib\ctypes\__init__.py", line 452, in LoadLibrary return self._dlltype(name) File "C:\Users\fjdks\AppData\Local\Programs\Python\Python39\lib\ctypes\__init__.py", line 374, in __init__ self._handle = _dlopen(self._name, mode) FileNotFoundError: Could not find module 'C:\Users\fjdks\AppData\Local\Programs\Python\Python39\lib\site-packages\pyzbar\libzbar-64.dll' (or one of its dependencies). Try using the full path with constructor syntax.
집에있는 오래된 PC에서는 문제없이 실행되는데 새로운 노트북에서만 자꾸 실행이 되지 않았습니다. PC의 파이썬 자체를 가져와서 노트북에서 실행하는 것도 실패했습니다. 그러던 도중 해결 방법을 찾았습니다.