Étant donné un tableau de taille n, la tâche consiste à ajouter un élément x dans ce tableau en Java. La taille du tableau ne peut pas être modifiée dynamiquement en Java, comme c'est le cas en C/C++. Par conséquent, pour ajouter un élément dans le tableau, l’une des méthodes suivantes peut être utilisée :
- En créant un nouveau tableau :
- Créez un nouveau tableau de taille n+1, où n est la taille du tableau d'origine.
- Ajoutez les n éléments du tableau d'origine dans ce tableau.
- Ajoutez le nouvel élément en n+1 ème position.
- Imprimez le nouveau tableau.
Vous trouverez ci-dessous la mise en œuvre de l’approche ci-dessus :
Java
rekha indien
// Java Program to add an element in an Array> > import> java.io.*;> import> java.lang.*;> import> java.util.*;> > class> GFG {> > >// Function to add x in arr> >public> static> int>[] addX(>int> n,>int> arr[],>int> x)> >{> >int> i;> > >// create a new array of size n+1> >int> newarr[] =>new> int>[n +>1>];> > >// insert the elements from> >// the old array into the new array> >// insert all elements till n> >// then insert x at n+1> >for> (i =>0>; i newarr[i] = arr[i]; newarr[n] = x; return newarr; } // Driver code public static void main(String[] args) { int n = 10; int i; // initial array of size 10 int arr[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; // print the original array System.out.println('Initial Array:
' + Arrays.toString(arr)); // element to be added int x = 50; // call the method to add x in arr arr = addX(n, arr, x); // print the updated array System.out.println('
Array with ' + x + ' added:
' + Arrays.toString(arr)); } }> |
>
bonjour tout le monde java
>
Output: Initial Array: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] Array with 50 added: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 50]>
Complexité temporelle : O(N)
Espace auxiliaire : O(N)
- En utilisant ArrayList comme stockage intermédiaire :
- Créé un Liste des tableaux avec le tableau d'origine, en utilisant commeListe() méthode.
- Ajoutez simplement l'élément requis dans la liste en utilisant ajouter() méthode
- Convertissez la liste en tableau en utilisant versArray() méthode
Java
essayez le bloc catch en java
// Java Program to add an element in an Array> > >import> java.io.*;> >import> java.lang.*;> >import> java.util.*;> > >class> GFG {> > >// Function to add x in arr> >public> static> Integer[] addX(>int> n, Integer arr[],>int> x)> >{> >int> i;> > >// create a new ArrayList> >List arrlist> >=>new> ArrayList(> >Arrays.asList(arr));> > >// Add the new element> >arrlist.add(x);> > >// Convert the Arraylist to array> >arr = arrlist.toArray(arr);> > >// return the array> >return> arr;> >}> > >// Driver code> >public> static> void> main(String[] args)> >{> > >int> n =>10>;> >int> i;> > >// initial array of size 10> >Integer arr[]> >= {>1>,>2>,>3>,>4>,>5>,>6>,>7>,>8>,>9>,>10> };> > >// print the original array> >System.out.println(>'Initial Array:
'> >+ Arrays.toString(arr));> > >// element to be added> >int> x =>50>;> > >// call the method to add x in arr> >arr = addX(n, arr, x);> > >// print the updated array> >System.out.println(>'
Array with '> + x> >+>' added:
'> >+ Arrays.toString(arr));> >}> >}> |
>
>
Output: Initial Array: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] Array with 50 added: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 50]>
Complexité temporelle : O(N)
Espace auxiliaire : O(N)