This shows you the differences between two versions of the page.
Previous revision | |||
— | fuss:java [2022/04/19 08:28] (current) – external edit 127.0.0.1 | ||
---|---|---|---|
Line 1: | Line 1: | ||
+ | ====== Creating a Repeated String ====== | ||
+ | <code java> | ||
+ | String s = " | ||
+ | String repeated = new String(new char[3]).replace(" | ||
+ | </ | ||
+ | |||
+ | result in '' | ||
+ | |||
+ | < | ||
+ | abcabcabc | ||
+ | </ | ||
+ | |||
+ | This is sort-of a low-level predecessor to printing repeats using [[fuss: | ||
+ | |||
+ | ====== Switch on String ====== | ||
+ | |||
+ | Compared to C#, Java does not allow to switch on string. However, this can still be done by wrapping the strings in an '' | ||
+ | |||
+ | <code java> | ||
+ | public class Switch { | ||
+ | public static enum Days { | ||
+ | Monday, | ||
+ | Tuesday, | ||
+ | Wednesday, | ||
+ | Thursday, | ||
+ | Friday, | ||
+ | Unknown | ||
+ | | ||
+ | }; | ||
+ | public static void main(String [] args) { | ||
+ | Days Day = Day.Monday; | ||
+ | switch (Day) { | ||
+ | case Monday: | ||
+ | case Tuesday: | ||
+ | case Wednesday: | ||
+ | case Thursday: | ||
+ | case Friday: | ||
+ | System.out.println(" | ||
+ | break; | ||
+ | default: | ||
+ | System.out.println(" | ||
+ | break; | ||
+ | } | ||
+ | } | ||
+ | } | ||
+ | |||
+ | </ | ||
+ | |||
+ | ====== Get All IP Addresses ====== | ||
+ | |||
+ | <code java> | ||
+ | import java.io.*; | ||
+ | import java.net.*; | ||
+ | import java.util.*; | ||
+ | import static java.lang.System.out; | ||
+ | |||
+ | public class ListAddrs { | ||
+ | public static void main(String args[]) throws SocketException { | ||
+ | InetAddress localhost = InetAddress.getLocalHost(); | ||
+ | InetAddress[] allMyIps = InetAddress.getAllByName(localhost.getCanonicalHostName()); | ||
+ | for (int i = 0; i < allMyIps.length; | ||
+ | out.printf(" | ||
+ | } | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | ====== Iterative Fibonacci Implementation ====== | ||
+ | |||
+ | <code java> | ||
+ | /////////////////////////////////////////////////////////////////////////// | ||
+ | // Copyright (C) 2016 Wizardry and Steamworks - License: GNU GPLv3 // | ||
+ | /////////////////////////////////////////////////////////////////////////// | ||
+ | public class Fibonacci { | ||
+ | public static void main(String[] args) { | ||
+ | int f0 = 0; | ||
+ | int f1 = 1; | ||
+ | |||
+ | int i = 0; | ||
+ | while(i < 10) { | ||
+ | int s = f0; | ||
+ | f0 = f1; | ||
+ | f1 = s + f1; | ||
+ | | ||
+ | System.out.println(s); | ||
+ | | ||
+ | i = i + 1; | ||
+ | } | ||
+ | } | ||
+ | } | ||
+ | </ |