21xrx.com
2024-11-22 08:02:32 Friday
登录
文章检索 我的文章 写文章
Java案例开发:10个让你快速入门的示例代码
2023-06-16 16:37:54 深夜i     --     --
Java 案例开发 快速入门

Java是一种广泛使用的高级编程语言,拥有丰富的开发工具和框架,实现了跨平台性和面向对象编程的优秀特性。对于初学者来说,通过实践案例来学习,才能真正掌握Java编程的精髓,本文将分享10个Java案例开发示例代码,供大家参考。

1.计算阶乘

public class Factorial {

  public static void main(String[] args) {

    int num = 5;

    int result = 1;

    for (int i = 1; i <= num; i++) {

      result *= i;

    }

    System.out.println("Factorial of " + num + " is " + result);

  }

}

2.计算斐波那契数列

public class Fibonacci {

  public static void main(String[] args) {

    int num = 10;

    int firstNum = 0;

    int secondNum = 1;

    int nextNum = 0;

    System.out.print(firstNum + " " + secondNum);

    for (int i = 2; i < num; i++) {

      nextNum = firstNum + secondNum;

      System.out.print(" " + nextNum);

      firstNum = secondNum;

      secondNum = nextNum;

    }

  }

}

3.冒泡排序

public class BubbleSort {

  public static void main(String[] args) {

    int[] arr = 7 ;

    int temp = 0;

    for (int i = 0; i < arr.length - 1; i++) {

      for (int j = 0; j < arr.length - i - 1; j++) {

        if (arr[j] > arr[j + 1]) {

          temp = arr[j];

          arr[j] = arr[j + 1];

          arr[j + 1] = temp;

        }

      }

    }

    for (int i : arr) {

      System.out.print(i + " ");

    }

  }

}

4.递归实现二分查找

public class BinarySearch {

  public static void main(String[] args) {

    int[] arr = 8;

    int target = 7;

    int result = binarySearch(arr, 0, arr.length - 1, target);

    if (result == -1) {

      System.out.println("Element not found");

    } else {

      System.out.println("Element found at index: " + result);

    }

  }

  public static int binarySearch(int[] arr, int left, int right, int target) {

    if (left <= right) {

      int middle = left + (right - left) / 2;

      if (arr[middle] == target)

        return middle;

       else if (arr[middle] > target) {

        return binarySearch(arr, left, middle - 1, target);

      } else {

        return binarySearch(arr, middle + 1, right, target);

      }

    }

    return -1;

  }

}

5.实现链表

class Node {

  int data;

  Node next;

  Node(int data)

    this.data = data;

    this.next = null;

}

public class LinkedList {

  Node head;

  public static void main(String[] args) {

    LinkedList list = new LinkedList();

    list.head = new Node(1);

    Node second = new Node(2);

    Node third = new Node(3);

    list.head.next = second;

    second.next = third;

    list.printList();

  }

  public void printList() {

    Node currentNode = head;

    while (currentNode != null) {

      System.out.print(currentNode.data + " ");

      currentNode = currentNode.next;

    }

  }

}

6.实现栈

public class Stack {

  private int capacity;

  private int top = -1;

  private int[] stackArray;

  public Stack(int capacity) {

    this.capacity = capacity;

    stackArray = new int[capacity];

  }

  public void push(int element) {

    if (top == capacity - 1) {

      System.out.println("Stack is full.");

    } else {

      top++;

      stackArray[top] = element;

      System.out.println(element + " added to stack.");

    }

  }

  public void pop() {

    if (top == -1) {

      System.out.println("Stack is empty.");

    } else {

      int popped = stackArray[top];

      top--;

      System.out.println(popped + " removed from stack.");

    }

  }

  public void displayStack() {

    for (int i = top; i >= 0; i--) {

      System.out.println(stackArray[i]);

    }

  }

}

7.使用泛型实现队列

public class Queue {

  private int capacity;

  private int front = 0;

  private int rear = -1;

  private int size = 0;

  private T[] queueArray;

  public Queue(int capacity) {

    this.capacity = capacity;

    queueArray = (T[]) new Object[capacity];

  }

  public void enqueue(T element) {

    if (isFull()) {

      System.out.println("Queue is full.");

    } else {

      rear++;

      if (rear == capacity) {

        rear = 0;

      }

      queueArray[rear] = element;

      size++;

    }

  }

  public T dequeue() {

    if (isEmpty()) {

      System.out.println("Queue is empty.");

      return null;

    } else {

      T dequeued = queueArray[front];

      queueArray[front] = null;

      front++;

      if (front == capacity) {

        front = 0;

      }

      size--;

      return dequeued;

    }

  }

  public boolean isFull() {

    return size == capacity;

  }

  public boolean isEmpty() {

    return size == 0;

  }

}

8.使用HashMap存储数据

import java.util.HashMap;

public class HashMapExample {

  public static void main(String[] args) {

    HashMap map = new HashMap ();

    map.put(1, "John");

    map.put(2, "Doe");

    map.put(3, "Steve");

    map.put(4, "Emily");

    System.out.println(map);

    map.remove(3);

    System.out.println(map);

    System.out.println(map.containsKey(4));

    System.out.println(map.containsValue("Doe"));

  }

}

9.使用HashSet去重

import java.util.HashSet;

public class HashSetExample {

  public static void main(String[] args) {

    int[] arr = { 1, 2, 3, 1, 4, 2, 5 };

    HashSet set = new HashSet ();

    for (int i : arr) {

      set.add(i);

    }

    System.out.println(set);

  }

}

10.使用Arrays工具类进行排序和查找

import java.util.Arrays;

public class ArraysExample {

  public static void main(String[] args) {

    int[] arr = { 3, 6, 2, 9, 1, 5, 4, 8, 7 };

    Arrays.sort(arr);

    System.out.println(Arrays.toString(arr));

    System.out.println(Arrays.binarySearch(arr, 7));

  }

}

  
  

评论区

{{item['qq_nickname']}}
()
回复
回复