Java Arrays
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class array {
public static void main(String[] args) {
int i,j,k;
i = j = k = 0;
System.out.println("demo hello");
int[] nums = new int[20];
double[] prices = { 5.5, 3, 4.2 };
boolean[] ans = { true,false,true };
char[] vowels = { 'a','e','i','o','u' };
String[] names = new String[10];
nums[1] = 1;
nums[2] = 2;
printarray (nums);
ArrayList list = new ArrayList<>();
Arrays.sort (nums);
Arrays.sort (nums);
int[][] rents = {{},{},{}};
// ArrayList can only hold Objects
// ArrayList is a dynamic array
List<Integer> myiary = new ArrayList<Integer>();
myiary.add(2);
}
public static void printarray (int[] a) {
int j = 0;
for (int i : a) {
System.out.printf("s[%d] = %d, " , j,i);
j++;
if (j % 5 == 0) {
System.out.println();
}
}
}
public static void printarray (String[] a) {
for (String i : a) {
System.out.printf("%s\n" , i);
}
}
}
Java String and Characters
public static void main(String[] args) throws IOException {
BufferedReader f = new BufferedReader(new FileReader("beads.in"));
PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("beads.out")));
StringTokenizer st = new StringTokenizer(f.readLine());
int n = Integer.parseInt(st.nextToken());
String s = f.readLine();
char[] sc = s.toCharArray();
System.out.println(sc);
char[] chars = {'t','h','i','s',' ','\u0024'}; // \u0024 is $ sign
String s = new String(chars);
System.out.println( s );
}
Insert and copy arrays
public static void copyArray(int[] array, int index) {
List<Integer> myiary = new ArrayList<Integer>();
myiary.add(2);
int[] tmp = new int[array.length-1];
System.arraycopy( array, 0, tmp, 0, index);
Integer[] iarray = new Integer[] { 2,3,4 };
List<Integer>> iiarray = new ArrayList<Integer>() {{
add(1);add(2);add(3);
}};
List<Integer> list = new ArrayList<Integer>(Arrays.asList(iarray));
List<Integer> iilist = new ArrayList<Integer>(iiarray);
ArrayList<String> slist = new ArrayList<String>(Arrays.asList(
new String[] {"One","Two","Three","Four"}));
}
Modify and merge elements in arrays
String a[] = { "A", "E", "I" };
String b[] = { "O", "U" };
List list = new ArrayList(Arrays.asList(a));
list.addAll(Arrays.asList(b));
Object[] c = list.toArray();
System.out.println(Arrays.toString(c));
int[] ia = {1,2,3};
int[] ib = {4,5,6};
int[] ic = new int[ ia.length + ib.length ];
// use loop to concatenate ia, ib to ic
Slice the array
public static void sliceArray() {
// intializing an array arr1
byte[] arr1 = new byte[] {5, 62, 15};
// printing the array arr1
System.out.println("Printing 1st array:");
for (int i = 0; i < arr1.length; i++) {
System.out.println(arr1[i]);
}
// copying array arr1 to arr2 with range from index 1 to 7
byte[] arr2 = Arrays.copyOfRange(arr1, 1, 7);
// printing the array arr2
System.out.println("Printing new array:");
for (int i = 0; i < arr2.length; i++) {
System.out.println(arr2[i]);
}
}
Remove elements in arrays
public static void removeElement(Integer[] array, int index) {
int[] tmp = new int[array.length-1];
System.arraycopy( array, 0, tmp, 0, index);
List<Integer> list = new ArrayList<Integer>(Arrays.asList(array));
list.add(3);
list.add(4);
for (int i = 0; i < list.size(); i++) {
if (list.get(i).equals(index)) {
list.remove(i);
}
}
}
Looping over the array
for (<type> i : array_of_type) {
}
char[] chars = {'t','h','i','s',' ','\u0024'};
// char to string
String s = new String(chars);
// string to char
char[] chars2 = s.toCharArray();
for (char c : chars) { System.out.print(c); }
System.out.println();
Using Java 8 Stream API
List<String> nums = Stream.of("ONE", "TWO", "THREE", "FOUR")
.collect(Collectors.toList());
Using Java 8 Collections API
List<String> places = Collections.unmodifiableList
(Arrays.asList("One", "Two", "Three"));
Using Java 9 API
List<String> sary = List.of("One", "Two", "Three");
List<Integer> iary = List.of(1,2,3);
Using Class for Arrays
import java.util.*;
import java.util.Arrays;
public class ary
{
public static void main (String[] args) {
int iary1[] = { 1,2,3,4 };
int iary2[] = { 5,6,7,8 };
int iary3[] = Arrays.copyOf (iary2, 10);
int iary4[] = Arrays.copyOfRange (iary3, 1, 3);
int iary5[] = { 6,2,7,0,1,3,4 };
System.out.println ("arrays:");
System.out.println ( Arrays.toString(iary1));
System.out.println ("compare two array:");
System.out.println ( Arrays.equals(iary1, iary1));
System.out.println ( Arrays.equals(iary1, iary4));
System.out.println ("array sort:");
Arrays.sort(iary5);
System.out.println ( Arrays.toString(iary5));
int tary1[][] = { { 9,8,7,6,5,4} };
int tary2[][] = { { 9,6,4 } };
System.out.println ( "Arrays comparison" );
System.out.println ( Arrays.deepEquals (tary1, tary2 ));
// Arrays.compare (iary1, iary2 );
// Arrays.compareUnsigned (iary1, iary2 );
// System.out.println ( Arrays.compare (iary1, iary2 ));
ArrayList list = new MyArrayList("a", "b", "c");
}
}
public class MyArrayList<T> extends ArrayList<T> {
public MyArrayList(T... values) {
super(Arrays.asList(values));
}
}
No comments:
Post a Comment