그만큼 세트() 의 방법 java.util.ArrayLis 티 클래스는 이 목록의 지정된 위치에 있는 요소를 지정된 요소로 바꾸는 데 사용됩니다.
통사론:
public E set(int index, E element)>
매개변수: 이 메소드는 다음 인수를 매개변수로 사용합니다.
- index- 대체할 요소의 인덱스 element- 지정된 위치에 저장될 요소
반환 값: 이 메소드는 요소 이전에 지정된 위치에 있었습니다.
예외: 이 방법은 IndexOutOfBoundsException 인덱스가 ArrayList의 크기 범위 내에 있지 않은 경우.
다음은 설명하기 위한 예입니다. 세트() 방법.
예시 1:
일반 보호 결함
문자열 비교 C#
// Java program to demonstrate> // set() method> // for Integer value> > import> java.util.*;> > public> class> GFG1 {> > public> static> void> main(String[] argv)> throws> Exception> > {> > try> {> > > // Creating object of ArrayList> > ArrayList> > arrlist => new> ArrayList();> > > // Populating arrlist1> > arrlist.add(> 1> );> > arrlist.add(> 2> );> > arrlist.add(> 3> );> > arrlist.add(> 4> );> > arrlist.add(> 5> );> > > // print arrlist> > System.out.println(> 'Before operation: '> > + arrlist);> > > // Replacing element at the index 3 with 30> > // using method set()> > int> i = arrlist.set(> 3> ,> 30> );> > > // Print the modified arrlist> > System.out.println(> 'After operation: '> > + arrlist);> > > // Print the Replaced element> > System.out.println(> 'Replaced element: '> > + i);> > }> > > catch> (IndexOutOfBoundsException e) {> > System.out.println(> 'Exception thrown: '> > + e);> > }> > }> }> |
>
파싱트 자바
>산출:
Before operation : [1, 2, 3, 4, 5] After operation : [1, 2, 3, 30, 5] Replaced element : 4>
예시 2: 을 위한 IndexOutOfBoundsException
0이 너무 많아
// Java program to demonstrate> // set() method> // for IndexOutOfBoundsException> > import> java.util.*;> > public> class> GFG1 {> > public> static> void> main(String[] argv)> throws> Exception> > {> > try> {> > > // Creating object of ArrayList> > ArrayList> > arrlist => new> ArrayList();> > > // Populating arrlist1> > arrlist.add(> 1> );> > arrlist.add(> 2> );> > arrlist.add(> 3> );> > arrlist.add(> 4> );> > arrlist.add(> 5> );> > > // print arrlist> > System.out.println(> 'Before operation : '> > + arrlist);> > > // Replacing element at the index 7 with 30> > // using method set()> > System.out.println(> '
Trying to Replace'> > +> ' the element at'> > +> ' (index>용량) '> );> > int> i = arrlist.set(> 7> ,> 30> );> > > // Print the modified arrlist> > System.out.println(> 'After operation: '> > + arrlist);> > > // Print the Replaced element> > System.out.println(> 'Replaced element: '> > + i);> > }> > > catch> (IndexOutOfBoundsException e) {> > System.out.println(> 'Exception thrown : '> + e);> > }> > }> }> |
>
>산출:
Before operation : [1, 2, 3, 4, 5] Trying to Replace the element at (index>용량) 예외 발생: java.lang.IndexOutOfBoundsException: 인덱스: 7, 크기: 5>