CodeGym學生最好的朋友是編程任務、Java 和任務驗證器。儘管如此,每個 padawan 開發人員都必須開始走出人跡罕至的道路,發明自己的迷你項目,並為面試做好準備。人們會認為面試中的實際 Java 任務與本課程中的任務完全相同。在大多數情況下都是如此,但有些公司喜歡問一些技巧性的問題或一些不熟悉的問題。為避免在緊張的面試中緊張不安,嘗試在家中自己解決類似的 Java 任務會很有幫助。
在本文中,我們將探索 5 個這樣的棘手任務。我們建議您先閱讀條件並嘗試自己解決。還有一件事:不要忘記每天完成本課程中的 Java 任務!

任務 1:從頭開始創建無限循環
提供了一個代碼塊。添加到它使循環無限。class ToInfinity {
public static void main(String[] args) {
// Insert code here
for (int i = start; i <= start + 1; i++) {
/* This should be an infinite loop, but don't change anything here */
}
}
}
“這不難, ”你說。您很可能多次發現自己處於這種情況:在完成 Java 任務時,您創建了一個無限循環並且不得不考慮如何擺脫它。好吧,這是相反的方式。這是棘手的部分:您不能更改循環本身或其退出條件。只有兩次迭代。但是,它們足以創建一個無限循環。看起來它應該只執行兩次迭代,但您可以使用溢出使其無限。你已經猜到怎麼做了嗎?
解決方案
由於溢出,是Java 中Integer.MAX_VALUE
an 可以存儲的最大值。int
如果您達到Integer.MAX_VALUE
並增加該值,您將回滾到Integer.MIN_VALUE
,或者更確切地說,回滾到可能的最低Integer
值。int
因此,要完成此 Java 任務,我們只需將變量的最大值減 1 即可start
。此任務的 Java 代碼:
class ToInfinity {
public static void main(String[] args) {
int start = Integer.MAX_VALUE - 1;
for (int i = start; i <= start + 1; i++) {
// Infinite loop
System.out.println(i); // Be sure we are in an infinite loop
}
}
}
會發生什麼?我們從 start=2147483645 (Integer.MAX_VALUE-1) 開始。在下一次迭代中,該值變為 2147483645、2147483646、-2147483648、-2147483647...等等。
任務 2:創建可執行註釋
好吧,我們到了!從第一節課開始,我們就听說評論不會被執行。這就是它們被稱為評論的原因。我們認為,對於 Java 程序員,即使是有經驗的程序員來說,這個任務的解決方案並不總是顯而易見的。但是,有一種棘手(但“合法”)的方法可以讓 JVM 執行註釋。你能感覺到我們要去哪裡嗎?猜猜看!解決方案
此任務的 Java 代碼:public class ExecutableComment {
public static void main(String[] args) {
// The comment below will be executed!
// \u000d System.out.println("executable comment");
}
}
如果我們將這段 Java 代碼輸入到 IDE 中,我們將得到以下結果:
executable comment
這是因為 Java 編譯器將 Unicode 字符解釋\u000d
為換行,並像這樣讀取我們的代碼:此任務的 Java 代碼,正如編譯器所解釋的那樣:
public class ExecutableComment {
public static void main(String[] args) {
// The comment below will be executed!
// \u000d
System.out.println("comment executed");
}
}
任務 3:創建命名循環
這是題為“球形奶牛的實用 Java 編程問題”系列的另一個成員。從某種意義上說,它甚至不清楚為什麼這是必要的:循環不太可能因為它沒有被賦予名稱而受到冒犯。無論如何,它之所以重要還有另一個原因:該語言允許您命名一個循環。解決方案
注意:這些“名稱”被某些人稱為“標籤”,不建議在實踐中使用。用於演示命名循環的 Java 代碼public class NamedLoop {
public static void main(String[] args) {
loop1:
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 5; j++) {
if (i == 3)
break loop1;
System.out.println("i = " + i + " j = " + j);
}
}
}
}
如果您運行該程序,將輸出以下內容:
i = 0 j = 0
i = 0 j = 1
i = 0 j = 2
i = 0 j = 3
i = 0 j = 4
i = 1 j = 0
i = 1 j = 1
i = 1 j = 2
i = 1 j = 3
i = 1 j = 4
i = 2 j = 0
i = 2 j = 1
i = 2 j = 2
i = 2 j = 3
i = 2 j = 4
您還可以使用關鍵字 continue 返回到命名循環的開頭。如有必要,您可以在嵌套循環中使用( break
or continue
)來使用. 這將有助於避免創建大量標誌並在語句中檢查它們以確定是繼續還是退出內部循環。 if-else
for
if-else
if-else
任務 4:在整數數組中查找一個重複項
給定一個包含1 到 100 中ArrayList
每個元素的整數數組(或 ,如果你願意的話)。該數組有一個且只有一個重複元素。Integer
你是怎麼找到它的?與前三種相比,Java 程序員更容易獲得此類任務。因為它是關於邏輯的,而不是你對語言中很少使用的微妙之處的了解。當您的大腦啟動或您意識到“我是一名程序員。我很聰明”時,您使用蠻力的第一個肆無忌憚的衝動會很快消失。唯一的問題是,在面試期間,當你處於壓力之下時,這可能不會發生。因此,在查看解決方案之前請先思考一下!
這是算法
計算從 1 到 100 的所有數字的總和。我們認為您知道如何做到這一點(例如,使用高斯的著名方法)。現在計算數組或 中元素的總和ArrayList
。並且...從第二個總和中減去第一個總和。答對了!結果數字是重複元素的值。 此任務的 Java 代碼使用ArrayList
.
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
public class FindDuplicate {
private static void findDuplicate(List<Integer> elements) {
int distinctSum = elements.stream().distinct().mapToInt(e -> e).sum();
// Find the sum of all list elements
int totalSum = elements.stream().mapToInt(e -> e).sum();
System.out.println("The repeated element is: " + (totalSum - distinctSum));
}
public static void main(String[] args) {
// Create a list of sequential elements in the interval [1..101).
List <Integer> elements = IntStream.range(1, 101).boxed().collect(Collectors.toList());
// Add the value 23 at index 53
elements.set(53, 23);
findDuplicate(elements);
}
}
另一種解決方案
import java.util.List;
import java.util.ArrayList;
public class Duplicate {
public int findDuplicateNumber(List<Integer> numbers) {
int highestNumber = numbers.size() - 1;
int total = getSum(numbers);
int duplicate = total - (highestNumber * (highestNumber + 1) / 2);
return duplicate;
}
public int getSum(List<Integer> numbers) {
int sum = 0;
for (int num : numbers) {
sum = sum + num;
}
return sum;
}
public static void main(String a[]) {
List <Integer> numbers = new ArrayList <Integer>();
for (int i = 1; i < 100; i++) {
numbers.add(i);
}
// Add a duplicate to the list
numbers.add(25);
Duplicate dn = new Duplicate();
System.out.println("The repeated element is: " + dn.findDuplicateNumber(numbers));
}
}
任務 5:在整數數組中查找非唯一重複項
如果上一個任務對你來說太簡單了,試試這個:給你一個從 1 到 100 的整數列表。其中有重複項(不止一個)。你如何找到出現多次的元素(找到元素並指出它們出現了多少次)?解決方案
這里合乎邏輯的解決方案是使用類似 HashMap 的東西,因為它將數據存儲在鍵值對中。 此任務的 Java 代碼:import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
public class SomeDuplicates {
private static void findDuplicates(List<Integer> elements) {
HashMap <Integer, Integer> duplicates = new HashMap <>();
// Use the following rule to populate the Map duplicates:
// the key is the element value, and the value is the number of times it occurs
elements.forEach(e -> duplicates.put(e, duplicates.get(e) == null ? 1 : duplicates.get(e) + 1));
// Remove any elements that don't occur more than once from the Map duplicates
// and store the result as a list (to make it easy to work with in the next step)
List <Map.Entry <Integer, Integer> >
result = duplicates.entrySet().stream().filter(d -> d.getValue() > 1).collect(Collectors.toList());
// Write the results for all elements to the result list
result.forEach(e -> System.out.println(String.format("Element %d occurs %d times", e.getKey(), e.getValue())));
}
public static void main(String[] args) {
List <Integer> elements = IntStream.range(1, 101).boxed().collect(Collectors.toList());
elements.set(97, 23);
elements.set(27, 51);
elements.set(99, 23);
findDuplicates(elements);
}
}
GO TO FULL VERSION