Vzhledem k poli o velikosti n je úkolem přidat prvek x do tohoto pole v Javě. Velikost pole nelze v Javě dynamicky měnit, jak se to dělá v C/C++. Chcete-li tedy přidat prvek do pole, lze provést jednu z následujících metod:
- Vytvořením nového pole:
- Vytvořte nové pole o velikosti n+1, kde n je velikost původního pole.
- Přidejte do tohoto pole n prvků původního pole.
- Přidejte nový prvek na n+1. pozici.
- Vytiskněte nové pole.
Níže je uvedena implementace výše uvedeného přístupu:
Jáva
celé číslo na řetězec
// 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)); } }> |
>
globální proměnné javascriptu
>
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]>
Časová složitost: O(N)
Pomocný prostor: O(N)
- Pomocí ArrayList jako přechodného úložiště:
- Vytvořit ArrayList s původním polem pomocí asList() metoda.
- Jednoduše přidejte požadovaný prvek do seznamu pomocí přidat() metoda
- Převeďte seznam na pole pomocí toArray() metoda
Jáva
// 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));> >}> >}> |
pyspark sql
>
>
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]>
Časová složitost: O(N)
Pomocný prostor: O(N)