の セット() の方法 java.util.ArrayLis t class は、このリスト内の指定された位置にある要素を指定された要素に置き換えるために使用されます。
構文:
public E set(int index, E element)>
パラメーター: このメソッドは次の引数をパラメータとして受け取ります。
- Index- 置換する要素のインデックス element- 指定された位置に格納される要素
戻り値: このメソッドは、 要素 あらかじめ指定された位置にあります。
例外: このメソッドはスローします IndexOutOfBoundsException インデックスが ArrayList のサイズ範囲内にない場合。
以下に例を示します。 セット() 方法。
例 1:
np.concatenate
文字列に変換する方法
// 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);> > }> > }> }> |
>
JavaScriptオンロードスクリプト
>出力:
Before operation : [1, 2, 3, 4, 5] After operation : [1, 2, 3, 30, 5] Replaced element : 4>
例 2: のために IndexOutOfBoundsException
unsigned int C プログラミング
// 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);> > }> > }> }> |
>
>出力: