当前位置:首页 > 服务端 > Java Interview Programming Questions

Java Interview Programming Questions

to be continue...

How to reverse a string in java?

//Using StringBuffer class
StringBuffer sbf = new StringBuffer("MyJava");

System.out.println(sbf.reverse());    //Output : avaJyM

//Using iterative method String str = "MyJava"; char[] strArray = str.toCharArray(); for (int i = strArray.length - 1; i >= 0; i--) { System.out.print(strArray[i]); //Output : avaJyM } //Using recursive method. static String recursiveMethod(String str) { if ((null == str) || (str.length() <= 1)) { return str; } return recursiveMethod(str.substring(1)) + str.charAt(0); } /* 1st Call —> recursiveMethod(“MyJava”) 2nd Call —> recursiveMethod(“yJava”) + “M” 3rd Call —> (recursiveMethod(“Java”) + “y”) + “M” 4th call —> ((recursiveMethod(“ava”) + “J”)+”y”) + “M” 5th Call —> (((recursiveMethod(“va”) + “a”) + “J”)+”y”) + “M” 6th Call —> ((((recursiveMethod(“a”) + “v”) + “a”) + “J”)+”y”) + “M” */

How to find duplicate characters in a string in java?

Java Interview Programming Questions _ JavaClub全栈架构师技术笔记

/*
Problem :
Write a java program to find duplicate characters and their count in a given string? 
For example, in a string “Better Butter”, duplicate characters and their count is t : 4, e : 3, r : 2 and B : 2.
*/
import java.util.HashMap;
import java.util.Set;
 
class DuplicateCharactersInString
{
    static void duplicateCharCount(String inputString)
    {
        //Creating a HashMap containing char as key and it's occurrences as value
 
        HashMap<Character, Integer> charCountMap = new HashMap<Character, Integer>();
 
        //Converting given string to char array
 
        char[] strArray = inputString.toCharArray();
 
        //checking each char of strArray
 
        for (char c : strArray)
        {
            if(charCountMap.containsKey(c))
            {
                //If char is present in charCountMap, incrementing it's count by 1
 
                charCountMap.put(c, charCountMap.get(c)+1);
            }
            else
            {
                //If char is not present in charCountMap,
                //putting this char to charCountMap with 1 as it's value
 
                charCountMap.put(c, 1);
            }
        }
 
        //Getting a Set containing all keys of charCountMap
 
        Set charsInString = charCountMap.keySet();
 
        System.out.println("Duplicate Characters In "+inputString);
 
        //Iterating through Set 'charsInString'
 
        for (Character ch : charsInString)
        {
            if(charCountMap.get(ch) > 1)
            {
                //If any char has a count of more than 1, printing it's count
 
                System.out.println(ch +" : "+ charCountMap.get(ch));
            }
        }
    }
 
    public static void main(String[] args)
    {
       duplicateCharCount("JavaJ2EE");
 
       duplicateCharCount("Fresh Fish");
 
       duplicateCharCount("Better Butter");
    }
}

Ref:https://java2blog.com/java-interview-programs/

Ref:http://www.java2novice.com/java-interview-programs/

Ref:https://javaconceptoftheday.com/java-interview-programs-with-solutions/

Ref:https://www.edureka.co/blog/interview-questions/java-interview-questions/

Ref:https://www.google.com/search?q=java+interview+programming+questions

Ref:https://legacy.gitbook.com/book/lrh1993/android_interview_guide/details

作者:Nick.Chung
来源链接:https://www.cnblogs.com/ncore/p/10387307.html

版权声明:
1、Java侠(https://www.javaxia.com)以学习交流为目的,由作者投稿、网友推荐和小编整理收藏优秀的IT技术及相关内容,包括但不限于文字、图片、音频、视频、软件、程序等,其均来自互联网,本站不享有版权,版权归原作者所有。

2、本站提供的内容仅用于个人学习、研究或欣赏,以及其他非商业性或非盈利性用途,但同时应遵守著作权法及其他相关法律的规定,不得侵犯相关权利人及本网站的合法权利。
3、本网站内容原作者如不愿意在本网站刊登内容,请及时通知本站(javaclubcn@163.com),我们将第一时间核实后及时予以删除。





本文链接:https://www.javaxia.com/server/124605.html

标签: Duplicate class
分享给朋友:

“Java Interview Programming Questions” 的相关文章