使用基本类型去创建一个类,可以添加元素和获取元素
-
- public class MyArray {
- private int[] elements;
- private int size;
-
- public MyArray() {
- elements = new int[10]; // 初始化一个大小为10的数组
- size = 0; // 初始元素个数为0
- }
-
- public void add(int element) {
- if (size == elements.length) {
- // 如果数组已满,则扩容
- int[] newElements = new int[elements.length * 2];
- System.arraycopy(elements, 0, newElements, 0, elements.length);
- elements = newElements;
- }
- elements[size] = element;
- size++;
- }
-
- public int get(int index) {
- if (index < 0 || index >= size) {
- throw new IndexOutOfBoundsException("Index out of range");
- }
- return elements[index];
- }
- public static void main(String[] args) {
- MyArray myArray = new MyArray();
- myArray.add(1);
- myArray.add(2);
- myArray.add(3);
-
- System.out.println(myArray.get(0)); // 输出:1
- System.out.println(myArray.get(1)); // 输出:2
- System.out.println(myArray.get(2)); // 输出:3
- }
- }
使用基本类型去创建一个单向链表类,可以添加元素和获取元素
- public class ListNode {
- private int val;
- private ListNode next;
-
- public ListNode(int val) {
- this.val = val;
- this.next = null;
- }
-
- public int getVal() {
- return val;
- }
-
- public void setVal(int val) {
- this.val = val;
- }
-
- public ListNode getNext() {
- return next;
- }
-
- public void setNext(ListNode next) {
- this.next = next;
- }
- }
-
- public class MyLinkedList {
- private ListNode head;
-
- public MyLinkedList() {
- head = null;
- }
-
- public void add(int element) {
- ListNode newNode = new ListNode(element);
- if (head == null) {
- head = newNode;
- } else {
- ListNode current = head;
- while (current.getNext() != null) {
- current = current.getNext();
- }
- current.setNext(newNode);
- }
- }
-
- public int get(int index) {
- ListNode current = head;
- for (int i = 0; i < index; i++) {
- if (current == null) {
- throw new IndexOutOfBoundsException("Index out of range");
- }
- current = current.getNext();
- }
- if (current == null) {
- throw new IndexOutOfBoundsException("Index out of range");
- }
- return current.getVal();
- }
- }
第一个线程打印10次a ,第二个线程打印10次吧,第三个线程打印10次c,三个线程交替打印abc
-
- public class PrintABC {
- private static final Object lock = new Object();
- private static int count = 0;
-
- public static void main(String[] args) {
- Thread threadA = new Thread(new PrintThread("A", 0));
- Thread threadB = new Thread(new PrintThread("B", 1));
- Thread threadC = new Thread(new PrintThread("C", 2));
-
- threadA.start();
- threadB.start();
- threadC.start();
- }
-
- static class PrintThread implements Runnable {
- private String name;
- private int id;
-
- public PrintThread(String name, int id) {
- this.name = name;
- this.id = id;
- }
-
- @Override
- public void run() {
- for (int i = 0; i < 10; i++) {
- synchronized (lock) {
- while (count % 3 != id) {
- try {
- lock.wait(); // 当前线程等待
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- }
-
- System.out.print(name);
- count++;
-
- lock.notifyAll(); // 唤醒其他等待的线程
- }
- }
- }
- }
- }
有一个string变量,如何找出字符串中出现次数最多的字符
- import java.util.HashMap;
- import java.util.Map;
-
- public class MostFrequentChar {
- public static char findMostFrequentChar(String str) {
- Map
charCount = new HashMap<>(); -
- // 统计每个字符出现的次数
- for (char c : str.toCharArray()) {
- if (charCount.containsKey(c)) {
- charCount.put(c, charCount.get(c) + 1);
- } else {
- charCount.put(c, 1);
- }
- }
-
- char mostFrequentChar = '\0';
- int maxCount = 0;
-
- // 找出出现次数最多的字符
- for (Map.Entry
entry : charCount.entrySet()) { - char c = entry.getKey();
- int count = entry.getValue();
-
- if (count > maxCount) {
- mostFrequentChar = c;
- maxCount = count;
- }
- }
-
- return mostFrequentChar;
- }
-
- public static void main(String[] args) {
- String str = "abracadabra";
- char mostFrequentChar = findMostFrequentChar(str);
- System.out.println("出现次数最多的字符: " + mostFrequentChar);
- }
- }