这些例子可以帮助Java初学者逐步提高编程技能。
想要观看完整代码案例,可以查看前一篇(1)的网址:Java编程练习题!(1)
中级篇
8. 数组最大子序列和 编写一个Java程序,找到一个整数数组中的连续子数组的最大和。
1public class MaxSubArraySum {
2 public static int maxSubArraySum(int[] nums) {
3 int currentSum = nums[0];
4 int maxSum = nums[0];
5
6 for (int i = 1; i < nums.length; i++) {
7 currentSum = Math.max(nums[i], currentSum + nums[i]);
8 maxSum = Math.max(maxSum, currentSum);
9 }
10 return maxSum;
11 }
12
13 public static void main(String[] args) {
14 int[] nums = {-2, 1, -3, 4, -1, 2, 1, -5, 4};
15 System.out.println("Max subarray sum: " + maxSubArraySum(nums));
16 }
17}
9. 字符串压缩 编写一个Java程序,将字符串中的重复字符压缩成计数形式。
1public class StringCompression {
2 public static String compress(String input) {
3 StringBuilder compressed = new StringBuilder();
4 int count = 1;
5
6 for (int i = 1; i <= input.length(); i++) {
7 if (i < input.length() && input.charAt(i) == input.charAt(i - 1)) {
8 count++;
9 } else {
10 compressed.append(input.charAt(i - 1)).append(count);
11 count = 1;
12 }
13 }
14 return compressed.toString().length() < input.length() ? compressed.toString() : input;
15 }
16
17 public static void main(String[] args) {
18 String original = "aabcccccaaa";
19 String compressed = compress(original);
20 System.out.println("Original: " + original);
21 System.out.println("Compressed: " + compressed);
22 }
23}
高级篇
10. 模拟银行账户 创建一个简单的银行账户类,包括存款、取款和查看余额的方法。
1public class BankAccount {
2 private double balance;
3
4 public BankAccount(double initialBalance) {
5 this.balance = initialBalance;
6 }
7
8 public void deposit(double amount) {
9 if (amount > 0) {
10 balance += amount;
11 }
12 }
13
14 public boolean withdraw(double amount) {
15 if (amount > 0 && amount <= balance) {
16 balance -= amount;
17 return true;
18 }
19 return false;
20 }
21
22 public double getBalance() {
23 return balance;
24 }
25
26 public static void main(String[] args) {
27 BankAccount account = new BankAccount(1000);
28 account.deposit(500);
29 System.out.println("Current balance: " + account.getBalance());
30
31 boolean success = account.withdraw(200);
32 if (success) {
33 System.out.println("Withdrawal successful. New balance: " + account.getBalance());
34 } else {
35 System.out.println("Insufficient funds.");
36 }
37 }
38}
11. 二叉树遍历 实现一个二叉树类,支持前序、中序和后序遍历。
1class TreeNode {
2 int value;
3 TreeNode left, right;
4
5 TreeNode(int item) {
6 value = item;
7 left = right = null;
8 }
9}
10
11public class BinaryTree {
12 TreeNode root;
13
14 public BinaryTree() {
15 root = null;
16 }
17
18 void preOrder(TreeNode node) {
19 if (node == null) return;
20 System.out.print(node.value + " ");
21 preOrder(node.left);
22 preOrder(node.right);
23 }
24
25 void inOrder(TreeNode node) {
26 if (node == null) return;
27 inOrder(node.left);
28 System.out.print(node.value + " ");
29 inOrder(node.right);
30 }
31
32 void postOrder(TreeNode node) {
33 if (node == null) return;
34 postOrder(node.left);
35 postOrder(node.right);
36 System.out.print(node.value + " ");
37 }
38
39 public static void main(String[] args) {
40 BinaryTree tree = new BinaryTree();
41 tree.root = new TreeNode(1);
42 tree.root.left = new TreeNode(2);
43 tree.root.right = new TreeNode(3);
44 tree.root.left.left = new TreeNode(4);
45 tree.root.left.right = new TreeNode(5);
46
47 System.out.println("Preorder traversal:");
48 tree.preOrder(tree.root);
49 System.out.println("\nInorder traversal:");
50 tree.inOrder(tree.root);
51 System.out.println("\nPostorder traversal:");
52 tree.postOrder(tree.root);
53 }
54}
12. 使用泛型的队列 实现一个使用泛型的队列类。
1import java.util.LinkedList;
2
3public class GenericQueue<T> {
4 private LinkedList<T> queue = new LinkedList<>();
5
6 public void enqueue(T item) {
7 queue.addLast(item);
8 }
9
10 public T dequeue() {
11 return queue.poll();
12 }
13
14 public boolean isEmpty() {
15 return queue.isEmpty();
16 }
17
18 public static void main(String[] args) {
19 GenericQueue<Integer> queue = new GenericQueue<>();
20 queue.enqueue(1);
21 queue.enqueue(2);
22 queue.enqueue(3);
23
24 while (!queue.isEmpty()) {
25 System.out.println(queue.dequeue());
26 }
27 }
28}