Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
fuss:java [2016/01/14 16:09] – [Iterative Fibonacci Implementation] officefuss: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 = "abc";
 +String repeated = new String(new char[3]).replace("\0", s);
 +</code>
 +
 +result in ''repeated'' containing:
 +
 +<code>
 +abcabcabc
 +</code>
 +
 +This is sort-of a low-level predecessor to printing repeats using [[fuss:csharp#linq_number_of_times|C# LINQ]].
 +
 +====== 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 ''enum'':
 +
 +<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("Weekdays");
 +        break;
 +      default:
 +        System.out.println("Unknown day");
 +        break;
 +    }
 +  }
 +}
 +
 +</code>
 +
 +====== 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; i++)
 +            out.printf("InetAddress: %s\n", allMyIps[i]);
 +    }
 +}
 +</code>
 +
 +====== 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;
 +        }
 +    }
 +}
 +</code>

fuss/java.1452787798.txt.bz2 · Last modified: 2016/01/14 16:09 by office

Access website using Tor Access website using i2p Wizardry and Steamworks PGP Key


For the contact, copyright, license, warranty and privacy terms for the usage of this website please see the contact, license, privacy, copyright.