자바에서 List에 Char [ ] 을 집어넣었는데 다시 빼낼때 Char [ ] 에 저장을 하려고하는데 어떻게 해야하나요 ?
조회수 2672회
temp =new char[10];
List Temp=new ArrayList();
Temp.add(temp);
//다른 객체 (List temp)
char[ ] Temp;
Temp =(char[ ]) temp.get(0);
오류발생 java.lang.IndexOutOfBoundsException
이런식으로 리스트에서 얻어온 char[ ] 형태의 배열을 다른객체의 char[ ] 배열로 옮길려고하는데 어떻게 해야하나요 ?
-
(•́ ✖ •̀)
알 수 없는 사용자 - 〉
2 답변
-
현재 예시가 같은 블록안에 있다면 IndexOutOfBoundsException 에러 조차 확인이 안됩니다.
예시를 정확하게 들어주셔야 답변해드리기가 쉬워요.
제가 이해하기엔 아래처럼 하시려는것 같은데요. 맞는지 모르겠네요
char[] tempArray = new char[10]; System.out.println(tempArray.hashCode()); List tempList = new ArrayList(); tempList.add(tempArray); char[] targetArray = (char[]) tempList.get(0); System.out.println(targetArray.hashCode());
-
질문의 내용을 정리해보면
List에 있는
char[]
배열을 다른 배열로 복사하려는 것으로 이해됩니다.char[] temp new char[10]; List<char[]> list = new ArrayList<>(); list.add(temp); // 이 후에 새로운 배열에 복사하려면... char[] copyTarget; char[] src= list.get(0); // 다음과 같이 새로운 배열의 크기를 기존 배열과 동일하게 생성한 후 copyTarget = new char[src.length]; // System.arraycopy 를 통해 복사합니다. System.arraycopy(src, 0, copyTarget, 0, copyTarget.length);
댓글 입력