summaryrefslogtreecommitdiffstats
path: root/src/de/fhswf/in/inf/java1/aufgabe04/Fill.java
diff options
context:
space:
mode:
authorStefan Suhren <suhren.stefan@fh-swf.de>2014-12-07 19:15:58 +0100
committerStefan Suhren <suhren.stefan@fh-swf.de>2014-12-07 19:18:03 +0100
commit9acea903216dbe371dd7b41cbf23b46a5732bcb4 (patch)
tree5a61491ad22e470db8151d2e07f163b011c72d6e /src/de/fhswf/in/inf/java1/aufgabe04/Fill.java
parent8a53a8fca255e4a84ca0e35dac925a223738b98a (diff)
downloadJava1-9acea903216dbe371dd7b41cbf23b46a5732bcb4.tar.gz
Java1-9acea903216dbe371dd7b41cbf23b46a5732bcb4.zip
Refactored the packagenames fo better sorting
Diffstat (limited to 'src/de/fhswf/in/inf/java1/aufgabe04/Fill.java')
-rw-r--r--src/de/fhswf/in/inf/java1/aufgabe04/Fill.java117
1 files changed, 117 insertions, 0 deletions
diff --git a/src/de/fhswf/in/inf/java1/aufgabe04/Fill.java b/src/de/fhswf/in/inf/java1/aufgabe04/Fill.java
new file mode 100644
index 0000000..b60c6c2
--- /dev/null
+++ b/src/de/fhswf/in/inf/java1/aufgabe04/Fill.java
@@ -0,0 +1,117 @@
+/**
+ *
+ */
+package de.fhswf.in.inf.java1.aufgabe04;
+
+import java.util.Arrays;
+
+/**
+ * Tests the different methods to created a prefilled String.
+ *
+ * @author $Author: $
+ * @version $Revision: $, $Date: $ UTC
+ */
+public final class Fill
+{
+ private Fill()
+ {
+ }
+
+ /**
+ *
+ * Creates a string of given length filled with the given char.
+ *
+ * @param length
+ * Length of the returned String.
+ * @param c
+ * Char the String is filled with.
+ * @return Returns the generated String.
+ */
+ public static String createStringFilledWith(int length, char c)
+ {
+ // String that will be returned
+ String result = "";
+ for (int i = 0; i < length; i++)
+ {
+ // Every iteration, a new string is created.
+ result += c;
+ }
+ // Return the build string
+ return result;
+ }
+
+ /**
+ *
+ * Creates a string of given length filled with the given char.
+ *
+ * @param length
+ * Length of the returned String.
+ * @param c
+ * Char the String is filled with.
+ * @return Returns the generated String.
+ */
+ public static String createStringFilledWith2(int length, char c)
+ {
+ // charArray that will be returned
+ char[] result = new char[length];
+
+ Arrays.fill(result, c);
+
+ // Return the build string
+ return result.toString();
+ }
+
+ /**
+ *
+ * Creates a string of given length filled with the given char.
+ *
+ * @param length
+ * Length of the returned String.
+ * @param c
+ * Char the String is filled with.
+ * @return Returns the generated String.
+ */
+ public static String createStringFilledWith3(int length, char c)
+ {
+ // String that will be returned
+ StringBuilder result = new StringBuilder(length);
+ for (int i = 0; i < length; i++)
+ {
+ // Every iteration, a new string is created.
+ result.append(c);
+ }
+ // Return the build string
+ return result.toString();
+ }
+
+ /**
+ *
+ * Calls the function createStringFilledWith() and measures its runtime.
+ *
+ * @param args
+ * Console line arguments.
+ */
+ public static void main(String[] args)
+ {
+ final int anzDurchlaefe = 100000000;
+
+ long start;
+ start = System.currentTimeMillis(); // Gets current time in �s
+ Fill.createStringFilledWith(anzDurchlaefe, 'x'); // Function call
+ // Calculates the time runtime of creatStringFilledWith()
+ System.out.println("Elapsed time: "
+ + (System.currentTimeMillis() - start) + " ms");
+
+ start = System.currentTimeMillis(); // Gets current time in �s
+ Fill.createStringFilledWith2(anzDurchlaefe, 'x'); // Function call
+ // Calculates the time runtime of creatStringFilledWith2()
+ System.out.println("Elapsed time: "
+ + (System.currentTimeMillis() - start) + " ms");
+
+ start = System.currentTimeMillis(); // Gets current time in �s
+ Fill.createStringFilledWith3(anzDurchlaefe, 'x'); // Function call
+ // Calculates the time runtime of creatStringFilledWith3()
+ System.out.println("Elapsed time: "
+ + (System.currentTimeMillis() - start) + " ms");
+ }
+}