Java Array

Kevin FOO
Apr 28, 2022

--

My cheat sheet of array related functions. I’ll add more along the way when I use more of it.

Declaring Array

int[] ia = new int[3];
ia[0] = 9;
ia[1] = 8;
ia[2] = 7;

Char Array To String

char[] ca = {'a', 'p', 'p', 'l', 'e'};
String s = String.valueOf(ca);
System.out.println(s);

Array Sort

String[] fruits = {"pear", "apple", "banana", "orange"};
Arrays.sort(fruits);
for(String fruit:fruits){
System.out.println(fruit);
}

Array List Sort

ArrayList<String> fruits = new ArrayList<String>();
fruits.add("orange");
fruits.add("pear");
fruits.add("apple");
fruits.add("banana");
Collections.sort(fruits);
System.out.println(fruits);
# size
System.out.println(fruits.size()); # 4
# get item 0
System.out.println(fruits.get(0)); # apple

< Back to all the stories I had written

--

--

Kevin FOO
Kevin FOO

Written by Kevin FOO

A software engineer, a rock climbing, inline skating enthusiast, a husband, a father.

No responses yet