什么是java中的array_push的等效代码?

时间:2012-01-06 20:52:50

标签: java android arrays equivalent

大家好我需要这个PHP代码的等效java代码:

<?php
  $stack = array("orange", "banana");
  array_push($stack, "apple", "raspberry");
  print_r($stack);
?>

输出是:

Array
(
    [0] => orange
    [1] => banana
    [2] => apple
    [3] => raspberry
)

3 个答案:

答案 0 :(得分:3)

您必须使用ArrayList

List<String> list = new ArrayList<String>();
list.add("orange");
list.add("banana");

答案 1 :(得分:0)

这将是ArrayList的add(Element)方法。

对于数组,您必须手动说出哪个索引,哪个元素:

String[] word = new String[5];
word[4] = "raspberry";

String[] word = {"orange","banana","raspberry","srtrawberry"};

答案 2 :(得分:0)

这将是一个非常接近的等价物:

import java.util.*;

List stack = new ArrayList(Arrays.asList("orange", "banana"));
Collections.addAll(stack, "apple", "raspberry");
System.out.println(stack);