✨
Java
  • 자바의 역사
  • Ch1
    • Before Start Java
      • History of Java
      • Feature of Java
      • API Documentation
      • Hello World!
      • eclipse shortcut
      • Eclipse IDE
      • GitHub to eclipse
  • Ch2
    • Variable
      • Variable
    • Variable Type
    • Character and String
    • Primitive type and range
    • printf and formatter
    • Scanner
    • Overflow of Int
    • Transition between different variable types
    • Object Array
  • CH3
    • Operator
  • CH4
    • 조건문과 반복문
    • if statement
    • switch
    • Math.random()
    • for statement
    • while statement
    • break, continue, named iterated statement
  • CH5
    • Array
    • 배열 활용
    • String Array
  • OOP
    • Intro
  • Class and Object
  • Make multiple Classes in one file
  • How to use Object
  • Object Array
  • Variable type according to declared location
  • Class Variable and Instance Variable
  • Method
  • Call Stack
  • Parameter
  • Static method(Class method) and Instance method
  • (Method)Overloading
  • Constructor
  • Constructor this()
  • Reference type variable "this"
  • Initialize variable
  • Inheritance
  • Composite
  • Single Inheritance, Object Class
  • (Method)Overriding
  • super, super()
  • package, class path
  • import, static import
  • modifier
  • access modifier
  • Encapsulation
  • Polymorphism
  • reference type transition
  • instanceof operator
  • Parameter polymorphism
  • Multiple object using one array
  • Abstract Class, Abstract Method
  • Creating Abstract Class
  • Interface
  • Interface Polymorphism
  • Interface Pros
  • Default method and static method
  • Inner Class
  • Anonymous Class
  • java.lang package and useful class
    • Object class
    • hashCode(), toString()
    • String class
    • StringBuffer class
    • StringBuilder class
    • Math class
    • Wrapper class
    • Number class
    • String to Number, String to Wrapper class
    • Auto-boxing and (auto)Unboxing
  • Collection Framework
    • Collections framework
    • List, Set, Map Interface
    • List의 removeAll() 과 clear() 비교
    • List Interface - ArrayList
    • How to view Java API source
    • List Interface - LinkedList
    • Stack and Queue
    • Iterator, ListIterator, Enumeration
    • Array
    • Comparator와 Comparable
    • Stack
    • String
    • String + char = String
    • String.toCharArray()
    • BufferedReader / BufferWriter
    • Scanner로 String 입력 - next( )와 nextLine( )
    • 공백없이 2차원배열 데이터 String으로 한번에 한줄씩 입력받기(문자/숫자)
    • 공백을 사이에 두고 빠른 2차원 배열 입출력
    • arr[index++]과 arr[index] index++의 차이
    • int와 long의 차이
    • Untitled
    • 타입 간의 변환
    • Array 와 ArrayList
    • valueOf()
    • Char
    • 변수, 객체 초기화(초기값 설정)
  • error troubleshooting
    • No enclosing instance of type
    • ASCII Code Table
    • java.lang.NumberFormatException
    • No enclosing instance..
  • reference
    • String을 생성하는 2가지 방법과 차이점
    • StackOverflowError(스택, 힙 메모리)
    • swtich-case 반복문
Powered by GitBook
On this page

Was this helpful?

  1. CH5

Array

  • 배열의 선언과 생성

선언 방법

선언 예

타입[ ] 변수이름;

int[ ] score;

String[ ] name;

타입 변수이름 [ ];

int score[ ];

String name[ ];

int[] score;//배열 선언
score = new int[5];//int형 값 5개를 저장할 수 있는 배열 생성

//배열의 선언과 생성을 한 번에!
int[] score = new int[5];
  • array.length : 배열의 길이(int형 상수)

배열은 한번 생성하면 그 길이를 바꿀 수 없다.

  1. 왜? 배열은 메모리에 연속적으로 값을 저장하기 때문에 배열을 한 번 선언하고 생성하고 나서 크기를 변경하게 되면 연속한 메모리 위치를 다시 찾아야하기 때문에 배열의 크기는 수정할 수 없다.

  2. 배열을 쓰다가 부족해지면? 새로운 더 큰 배열을 생성하여 기존의 배열을 복사한다.

  • 배열의 초기화

int[] score = {50, 60, 70, 80, 90};//new int[] 생략가능!

int[] score;
score = {50,60,70,80,90};//에러! new int[] 생략할 수 없다!
score = new int[] {50,60,70,80,90};//OK!
  • 배열의 출력

int[] iArr = {100,95,80,70,60};
char[] chArr = {'a','b','c','d'};
System.out.println(chArr);//abcd출력된다.
System.out.println(iArr);//X [I@14318bb 와 같은 형식의 문자열 출력.
for(int i=0; iArr.length; i++) {
    System.out.println(iArr[i]);
}

배열의 요소를 출력할 때, Arrays.toString() 이 많이 쓰인다. Arrays라는 클래스는 배열에 쓰이는 메소드들을 제공하는데 여기서는 toString()이라는 메소드로 배열의 요소들을 문자열로 반환해준다. Arrays를 사용하기 위해서는 import문을 추가해줘야한다! 추가해주지 않으면 Arrays에 빨간 줄이 생기는데, ctrl+shift+o 를 누르면 import 문이 추가된다!

import java.util.Arrays;//ctrl+shift+o
...
System.out.prinln(Arrays.toString(iArr));

Previousbreak, continue, named iterated statementNext배열 활용

Last updated 4 years ago

Was this helpful?