Skip to content

Tag: array

Java, printing arrays

As I keep forgetting, this post is to remind me that Java Java doesn’t have a pretty toString() for arrays objects, and it does for Lists.

[java]import java.util.List;
import java.util.ArrayList;
import java.util.Arrays;

public class ListsExample {
public static void main (String args[]) {
// as an array
String[] list1 = {"a","b","c"};
System.out.println(Arrays.toString(list1));

// as an List provided by Arrays
List<String> list2 = Arrays.asList("d", "e", "f");
System.out.println(list2);

// as an implementation of the interface List
// ArrayList (could also be LinkedList, Vector, etc)
List<String> list3 = new ArrayList<String>();
list3.add("g");
list3.add("h");
list3.add("i");
System.out.println(list3);
}
}
[/java]

The output is:

[a, b, c]
[d, e, f]
[g, h, i]

PHP: array, all elements but first

$bric = array("Brazil", "Russia", "India", "China"); 
$ric = $bric; // array copy
$br = array_shift($ric); // left shift at $ric. $br stores "Brazil" 
print_r($bric); // $bric remains the same
print_r($ric); // $ric lost "Brazil"

Output:

Array
(
    [0] => Brazil
    [1] => Russia
    [2] => India
    [3] => China
)
Array
(
    [0] => Russia
    [1] => India
    [2] => China
)

Reference: PHP array_shift at php.net.