• 遇到的题目


    使用基本类型去创建一个类,可以添加元素和获取元素

    1. public class MyArray {
    2. private int[] elements;
    3. private int size;
    4. public MyArray() {
    5. elements = new int[10]; // 初始化一个大小为10的数组
    6. size = 0; // 初始元素个数为0
    7. }
    8. public void add(int element) {
    9. if (size == elements.length) {
    10. // 如果数组已满,则扩容
    11. int[] newElements = new int[elements.length * 2];
    12. System.arraycopy(elements, 0, newElements, 0, elements.length);
    13. elements = newElements;
    14. }
    15. elements[size] = element;
    16. size++;
    17. }
    18. public int get(int index) {
    19. if (index < 0 || index >= size) {
    20. throw new IndexOutOfBoundsException("Index out of range");
    21. }
    22. return elements[index];
    23. }
    24. public static void main(String[] args) {
    25. MyArray myArray = new MyArray();
    26. myArray.add(1);
    27. myArray.add(2);
    28. myArray.add(3);
    29. System.out.println(myArray.get(0)); // 输出:1
    30. System.out.println(myArray.get(1)); // 输出:2
    31. System.out.println(myArray.get(2)); // 输出:3
    32. }
    33. }

    使用基本类型去创建一个单向链表类,可以添加元素和获取元素

    1. public class ListNode {
    2. private int val;
    3. private ListNode next;
    4. public ListNode(int val) {
    5. this.val = val;
    6. this.next = null;
    7. }
    8. public int getVal() {
    9. return val;
    10. }
    11. public void setVal(int val) {
    12. this.val = val;
    13. }
    14. public ListNode getNext() {
    15. return next;
    16. }
    17. public void setNext(ListNode next) {
    18. this.next = next;
    19. }
    20. }
    21. public class MyLinkedList {
    22. private ListNode head;
    23. public MyLinkedList() {
    24. head = null;
    25. }
    26. public void add(int element) {
    27. ListNode newNode = new ListNode(element);
    28. if (head == null) {
    29. head = newNode;
    30. } else {
    31. ListNode current = head;
    32. while (current.getNext() != null) {
    33. current = current.getNext();
    34. }
    35. current.setNext(newNode);
    36. }
    37. }
    38. public int get(int index) {
    39. ListNode current = head;
    40. for (int i = 0; i < index; i++) {
    41. if (current == null) {
    42. throw new IndexOutOfBoundsException("Index out of range");
    43. }
    44. current = current.getNext();
    45. }
    46. if (current == null) {
    47. throw new IndexOutOfBoundsException("Index out of range");
    48. }
    49. return current.getVal();
    50. }
    51. }

    第一个线程打印10次a ,第二个线程打印10次吧,第三个线程打印10次c,三个线程交替打印abc

    1. public class PrintABC {
    2. private static final Object lock = new Object();
    3. private static int count = 0;
    4. public static void main(String[] args) {
    5. Thread threadA = new Thread(new PrintThread("A", 0));
    6. Thread threadB = new Thread(new PrintThread("B", 1));
    7. Thread threadC = new Thread(new PrintThread("C", 2));
    8. threadA.start();
    9. threadB.start();
    10. threadC.start();
    11. }
    12. static class PrintThread implements Runnable {
    13. private String name;
    14. private int id;
    15. public PrintThread(String name, int id) {
    16. this.name = name;
    17. this.id = id;
    18. }
    19. @Override
    20. public void run() {
    21. for (int i = 0; i < 10; i++) {
    22. synchronized (lock) {
    23. while (count % 3 != id) {
    24. try {
    25. lock.wait(); // 当前线程等待
    26. } catch (InterruptedException e) {
    27. e.printStackTrace();
    28. }
    29. }
    30. System.out.print(name);
    31. count++;
    32. lock.notifyAll(); // 唤醒其他等待的线程
    33. }
    34. }
    35. }
    36. }
    37. }

    有一个string变量,如何找出字符串中出现次数最多的字符

    1. import java.util.HashMap;
    2. import java.util.Map;
    3. public class MostFrequentChar {
    4. public static char findMostFrequentChar(String str) {
    5. Map charCount = new HashMap<>();
    6. // 统计每个字符出现的次数
    7. for (char c : str.toCharArray()) {
    8. if (charCount.containsKey(c)) {
    9. charCount.put(c, charCount.get(c) + 1);
    10. } else {
    11. charCount.put(c, 1);
    12. }
    13. }
    14. char mostFrequentChar = '\0';
    15. int maxCount = 0;
    16. // 找出出现次数最多的字符
    17. for (Map.Entry entry : charCount.entrySet()) {
    18. char c = entry.getKey();
    19. int count = entry.getValue();
    20. if (count > maxCount) {
    21. mostFrequentChar = c;
    22. maxCount = count;
    23. }
    24. }
    25. return mostFrequentChar;
    26. }
    27. public static void main(String[] args) {
    28. String str = "abracadabra";
    29. char mostFrequentChar = findMostFrequentChar(str);
    30. System.out.println("出现次数最多的字符: " + mostFrequentChar);
    31. }
    32. }

  • 相关阅读:
    基于 SM3 的密钥派生函数 (KDF):国密合规的安全密钥生成方案
    微服务架构10个最重要的设计模式,带你了解,完全熟悉
    mindspore用户自定义数据问题
    Java方法概念/方法的定义与调用/形参与实参/方法的注意事项
    高效使用表的.frm和.idb文件备份MySQL表
    排序算法系列二:归并排序、快速排序
    算法---------空间复杂度
    3D Gaussian Splatting for Real-Time Radiance Field Rendering(慢慢啃,还是挺复杂的)
    口袋参谋:一键查询任意买家旺旺号,规避被降权风险!
    ChatGPT:使用FastJSON库关闭JSON引用检测的方法
  • 原文地址:https://blog.csdn.net/qq_63431773/article/details/134097578