// Two stacks s1 Original and s2 as Temp one
private Stack<Integer> s1 = new Stack<Integer>();
private Stack<Integer> s2 = new Stack<Integer>();
/*
* Here we insert the data into the stack and if data all ready exist on
* stack than we copy the entire stack s1 to s2 recursively and push the new
* element data onto s1 and than again recursively call the s2 to pop on s1.
*
* Note here we can use either way ie We can keep pushing on s1 and than
* while popping we can remove the first element from s2 by copying
* recursively the data and removing the first index element.
*/
public void insert( int data )
{
if( s1.size() == 0 )
{
s1.push( data );
}
else
{
while( !s1.isEmpty() )
{
s2.push( s1.pop() );
}
s1.push( data );
while( !s2.isEmpty() )
{
s1.push( s2.pop() );
}
}
}
public void remove()
{
if( s1.isEmpty() )
{
System.out.println( "Empty" );
}
else
{
s1.pop();
}
}
Вы можете запустить и увидеть, что array_push в некоторых случаях работает медленнее
http://snipplr.com/view/759/speed-test-arraypush-vs-array/
запустите ваш код. наслаждайтесь
Зависит ...
В документации сказано ,
«Если вы используете array_push () для добавить один элемент в массив, лучше использовать $ array [] =, потому что в этом случае нет накладных расходов на вызов функции ».
Источник: http://us2.php.net/array_push
Итак, все сводится к тому, сколько данных вы хотите втиснуть в этот массив в любой конкретный момент. Кроме того, существует резервный вариант: если массив, на который имеется ссылка, не существует, когда вы вызываете его с помощью array_push, вы столкнетесь с ошибкой. Если вы используете $ array [], массив будет создан за вас.