안녕하세요 자바에러좀 도와주세요...

안녕하세요 ASP프로그램을 하다가 회사다니면서 학원에 다니면서 JAVA를 배우고 있습니다

반복문까진 이해했는데..

배열에 대해 공부하는데..

자꾸 에러가 나서요...

컴파일은 되는데 실행에서 안됩니다.

아래는 소스 입니다..

class TestArray{
// 각 배열요소의 값을 1씩 증가시키는 메소드
static void incrementArray(int[] tmpArray){
for(int i=0;i=tmpArray.length;i++){
//각배열의 요소를 잃어서 1증가
//tmpArray[i]++;
/*혹은
tmpArray[i] +=1;
tmpArray[i] = tmpArray[i] +1;
*/
tmpArray[i] = tmpArray[i] +1;
}

}
// 각 배열요소의 값을 출력하는 메소드
static void printArray(int[] tmpArray){
//다른 형태의 For문

//enhanced for문이라고 하는데 JDK5.0부터 생김

for(int i : tmpArray){//tmpArray여기에 들어있는 인트값 하나
System.out.println(i);
}
/*
int i : 나열형 자원에 포함된 데이터의 하나를 나타내는 변수
tmpArray : 배열이나 나열형의 자원.
동일한 자원을 포함하고 있는 객체
*/
}
public static void main(String args[]){
int[] myIntArray = {1,3,5};
incrementArray(myIntArray);
printArray(myIntArray);
}

}

에러내용은

---------- Capture Output ----------
C:\jdk1.6.0\bin\java.exe TestArray
Exception in thread main java.lang.ArrayIndexOutOfBoundsException: 3
at TestArray.incrementArray(TestArray.java:11)
at TestArray.main(TestArray.java:32)
Terminated with exit code 1.염치없지만 한가지만 더 물어보겠습니다..

아래 코드는 아예 컴파일 자체가 에러가 납니다.

/**
dog배열을 만들어 보자

*/

public class DogArray{
public static void main(String args[]){
//개 배열을 만들자... 개집을 샀다라고 하자
Dog dogHouse[] = new Dog[2];
dogHouse[0] = new Dog();
dogHouse[1] = new Dog();
//2개 공간이 있는집을 마련했다
//그러나 개는 아직 없다
//개도 없는 빈공간에 대고 이름을 적어보자.
dogHouse[0].setName(benzi);
}
}
역시 배열을 만드는 건데

에러내용은

---------- Capture Output ----------
C:\jdk1.6.0\bin\javac.exe -d . DogArray.java
DogArray.java:9: cannot find symbol
symbol : class Dog
location: class DogArray
Dog dogHouse[] = new Dog[2];
^
DogArray.java:9: cannot find symbol
symbol : class Dog
location: class DogArray
Dog dogHouse[] = new Dog[2];
^
DogArray.java:10: cannot find symbol
symbol : class Dog
location: class DogArray
dogHouse[0] = new Dog();
^
DogArray.java:11: cannot find symbol
symbol : class Dog
location: class DogArray
dogHouse[1] = new Dog();
^
4 errors

Terminated with exit code 1.
와 진짜 어렵네요.. 자바...

ASP하곤 차원이 틀린거 같아요...

한수 부탁드립니다.

댓글 2

  • 단순드립 2025.10.28

    와 ...
    와우~~~ 감사합니다...

  • 연블랙 2025.10.28

    첫번째 질문은 = 비교 수식에서 =가 들어가서 i가 3까지 올라가므로 나타는증상이네요.
    두번째 질문은 Dog클래스를 정의해 주시지 않아서 그런것 같습니다.
    public class TestArray{
    public static void main(String args[]){
    //개 배열을 만들자... 개집을 샀다라고 하자
    Dog dogHouse[] = new Dog[2];
    dogHouse[0] = new Dog();
    dogHouse[1] = new

프로그램 개발일반 다른 글

목록