簡體   English   中英

Java程序無需使用任何內置方法即可讀取字符串和字符並顯示給定字符的最后一次出現的索引

[英]java program to read a String and a character and display the index of the last occurence of the given character without using any built in method

這是我設計的程序,用於查找給定字符的第一個匹配項,但是我想查找最后一個匹配項的邏輯。

import java.util.*;

public class Test77 {

  public static void main(String args[]) {
    Scanner sc = new Scanner(System.in);

    System.out.printf("\nEnter a String : ");
    String str = sc.nextLine();

    char arr[] = new char[str.length()];

    System.out.printf("\nEnter a Character : ");
    char ch = sc.next().charAt(0);


    for(int i = 0; i < str.length(); i++) 
    {
      arr[i] = str.charAt(i);
    }


    for (int i = 0; i < arr.length; i++) 
    {
      if (arr[i] == ch) 
      {
        System.out.println(i);
        break;
      }
    }
  }
}

一種簡單的方法:在循環中找到字符時不要中斷。 只需存儲其索引。

  1. 創建一個變量來存儲最后找到的索引
int lastIndex= -1;
for (int i = 0; i < arr.length; i++) {           
    if (arr[i] == ch) {
        System.out.println(i);
        //break;// remove this break.
        lastIndex=i;
    }
}
if (lastIndex > -1) {
    // this is your last occurrence.
}
  1. 另一種選擇是反向運行循環。 :)

遍歷整個數組並將位置保存在變量中。

import java.util.*;

public class Test77 {

  public static void main(String args[]) 
{

    Scanner sc = new Scanner(System.in);

    System.out.printf("\nEnter a String : ");
    String str = sc.nextLine();

    char arr[] = new char[str.length()];

    System.out.printf("\nEnter a Character : ");
    char ch = sc.next().charAt(0);


    for(int i = 0; i < str.length(); i++)
    {
        arr[i] = str.charAt(i);
    }

    int position = -1:
    for (int i = 0; i < arr.length; i++) 
    {
       if (arr[i] == ch) 
       {
        position = i;
       }
     }
     System.out.println("" + position)   ;

  }

}

我想糾正程序中的一些問題,並幫助您了解更好的方法

  1. 您在System.out.printf()方法中使用了\\n Java為您提供System.out.println()方法來執行此操作
  2. 您創建了一個char數組char arr[] = new char[str.length()]; 我認為沒有必要。 您可以使用String.charAt()方法直接檢查字符
  3. 由於您有興趣顯示給定字符的最后一次出現的索引,因此應該從頭開始進行迭代,以獲得更好的性能

示例:想象一下輸入字符串aaaabbbbbbbbbbbb...100's of b's in between.....bbbbbbbb 從頭開始迭代,您必須迭代到char數組的末尾以確定b的最后一次出現。 從背面進行迭代,您只需進行一次迭代即可,從而節省了時間。

話雖如此,這是更好的解決方案

package test;

import java.util.Scanner;

public class Test77 {

    public static void main(String args[]) {

        Scanner sc = new Scanner(System.in);

        System.out.println("Enter a String : ");
        String str = sc.nextLine();

        System.out.println("Enter a Character : ");
        char ch = sc.next().charAt(0);

        for (int i = str.length()-1; i >= 0; i--) {
            if (str.charAt(i) == ch) {
                System.out.println(i);
                break;
            }
        }

    }
}

注意:您的問題是不使用內置方法。 我認為這只是確定最后一個索引出現。 否則,您絕對應該使用諸如length()charAt()

像初次檢查器一樣執行此操作,但請反轉循環:

for (int i = arr.length - 1; i >= 0; i--) {
  if(arr[i] == ch){
    position = i;
    break;
  }
}

就像您的代碼一樣,只需反轉方向即可(如果您願意的話)是“首次出現后退”索引。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM