Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * Point of this is to cover all the basics you will need to remember for the competition.
- * Nothing challenging, just all the core stuff.
- */
- import java.util.Arrays;
- import java.util.Collections;
- import java.util.Comparator;
- import java.util.Scanner;
- public class template {
- public static void main(String args[]){
- // Type syso then press ctrl+space to add the output line
- System.out.println("Hello world!");
- int x = 3;
- String s = "String";
- char c = 'c';
- float f = 1.2345f; // Need the f to explicitly create a float (32-bits precision)
- double d = 1.2345; // 64-bits
- boolean b = true;
- System.out.println("Output with variable concatenated: " + d);
- System.out.print("Output without new line added by default.\nNew line\n");
- // Object used to read input
- Scanner in = new Scanner(System.in);
- // Get an int
- System.out.print("Type in a number: ");
- int y = in.nextInt();
- // This clears the line. Essential this is used after reading anything in other than the whole line.
- in.nextLine();
- System.out.print("Type in a string: ");
- String str = in.nextLine(); // Use this to get the whole line.
- System.out.println("Number entered: " + y + ", string entered: \"" + str + "\"");
- System.out.print("Press enter to continue.");
- in.nextLine();
- // Arrays- type name[] = new type[size]
- // When you know the size
- int xs[] = new int[10];
- // When you don't know the size
- int ys[][];
- // Re-assigning a variable for multiple arrays of different sizes
- ys = new int[3][4];
- ys = new int[5][7];
- System.out.println("Filling array xs with values:");
- // Fill it with descending values
- for(int i=0;i<xs.length;i++){
- xs[i] = xs.length - i;
- System.out.print(xs[i] + " ");
- }
- System.out.println();
- // Sort the array in ascending order
- Arrays.sort(xs);
- // Output array
- System.out.println("\nSorted array: ");
- for(int i=0; i<xs.length;i++){
- System.out.print(xs[i] + " ");
- }
- System.out.println();
- // Sorting in any other order is more difficult.
- // One way is to copy our int array to an Integer array
- Integer[] is = new Integer[xs.length];
- for(int i=0; i<is.length; i++){
- is[i] = Integer.valueOf(xs[i]);
- }
- // Now sort in descending order
- Arrays.sort(is, Collections.reverseOrder());
- // Output array
- System.out.println("\nSorted array: ");
- for(int i=0; i<is.length;i++){
- System.out.print(is[i] + " ");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement