본문 바로가기

반응형

Programming/Java

(34)
Dynamic Arrays in Java Problems with Arrays Fixed size Can pick size at runtime But size can't just be changed So we run out of room We want an Array That can grow Elements of Dynamic Arrays A list of items Accessible by index efficiently - constant time can grow arbitrarily long We need The arrya to store the data in The capacity of the arrya (already available in Java) The number of items currently stored in the arr..
BufferedReader와 BufferedWriter 사용법 BufferedReader :Scanner와 유사. ​ Bufferedwriter :System.out.println();과 유사 Scanner와 System.out.println()보다 속도 측면에서 훨씬 빠르다. 입력된 데이터가 바로 전달되지 않고 버퍼를 거쳐 전달되어 데이터의 처리 효율성을 높일 수 있다. 많은 양의 데이터를 처리할 때 유효하다. 비유하면 우리가 물건을 옮길 때 하나씩 옮기는 것이 아니라, 버퍼에 용량만큼 담았다가 한 꺼번에 전달하는 것이기 때문에 시간을 아낄 수 있는 것이다. 다만, bufferedReader는 enter만 경계로 인식하고 받은 데이터가 String으로 고정되어 입력받은 데이터를 가공하는 작업이 필요한 경우가 많다. 사용하기 전에 아래를 import해줘야 한다. 컨..
ArrayList functions import java.util.ArrayList; public class Array { public static void main(String[] args) { // ArrayList =a resizable array // Elements can be added and removed after compilation phase // store reference data types ArrayList food = new ArrayList(); // 이렇게 더할 수 있다. food.add("pizza"); food.add("hamburger"); food.add("hotdog"); // set function을 사용하면 이렇게 대체할 수 있다. food.set(0, "sushi"); // 이렇게 삭제할 수도 있..
생성자와 Getter Setter // 아래는 붕어빵 생산을 위한 붕어빵 틀이라고 보면 된다. public class User { //public User() { // //} 따로 생성자를 생성하지 않으면 자동적으로 생성되는 기본생성자 String name; // 이름 int age; // 나이 String hobby; // 취미 // 다른 클래스에서 위 생성자를 받도록 하기 위해서는 아래처럼 준비해줘야 한다. // 현재 위의 상태는 생성자를 선언만 해둔 상태라고 이해하면 됨. // this는 붙히지 않아도 되는데 파라미터값과 비슷할 때 구분을 위해 사용하기도 함. // alt shift s 단축키를 누르면 바로 생성할 수 있다. public User (String _name, int _age, String _hobby) { this.n..
java 제네릭 package org.opentutorials.javatutorials.generic; class Person{ public T info; } public class GenericDemo { public static void main (String[] args) { Person p1 = new Person(); Person p2 = new Person(); } }
ArrayList<datatype> al = new ArrayList<datatype>(); package org.opentutorials.javatutorials.collection; import java.util.ArrayList; public class ArrayListDemo { public static void main(String[] args) { String[] arrayObj = new String[2]; arrayObj[0] = "one"; arrayObj[1] = "two"; // arrayObj[2] = "one"; 오류 발생 for(int i=0; i < arrayObj.length; i++) { System.out.println(arrayObj[i]); } // ArrayList al = new ArrayList(); // ArrayList의 데이터타입을 이렇게 정해둘 수..
overriding의 조건 package org.opentutorials.javatutorials.overriding.example1; class Calculator { int left, right; public void setOprands(int left, int right) { this.left = left; this.right = right; } public void sum() { System.out.println(this.left + this.right); } public void avg() { System.out.println((this.left + this.right) / 2); } } class SubstractionableCalculator extends Calculator { public void sum() { S..
상속과 생성자 (기본생성자와 super) package org.opentutorials.javatutorials.Inheritance.example3; class Calculator { int left, right; // "여기" // 기본생성자를 명시적으로 선언해줘야 한다. // public Calculator(){} public Calculator(int left, int right){ this.left = left; this.right = right; } public void setOprands(int left, int right) { this.left = left; this.right = right; } public void sum() { System.out.println(this.left + this.right); } public vo..

반응형