`
iloveflower
  • 浏览: 78973 次
社区版块
存档分类
最新评论
  • iloveflower: 呵呵。好好学习。。。。。。。。。。。。
    java 读书
  • Eric.Yan: 看了一点,不过是电子版的……你这一说到提醒我了,还要继续学习哈 ...
    java 读书

programming with String

 
阅读更多
Question:

1) Write code to check a String is palindrome or not?

以下是我一开始写的CODE,后来仔细一看就有很多问题了:

public class CheckStringPalindrome {
String original;

public CheckStringPalindrome(String inputStr) {
this.original = inputStr;
}

private boolean checkStrNotNull() {
if ( this.original == null )
return false;
else
return true;
}

private String reverseStr() {
int length = original.length();
String reverse="";
for( int iCnt=0; iCnt < length; iCnt++ ) {
reverse = reverse + original.charAt(length-iCnt-1);
//这里没注意String is immutable, so here will generate a lot String objects.
//reverse should be a StringBuffer
}
return reverse;
}

private boolean checkStrEquals(String reverse) {
if ( reverse.equals(original)) {
return true;
}
else
return false;
}

public boolean checkStrPalinddrome() {
String reverse = "";
if ( checkStrNotNull() ) {
reverse = reverseStr();
if ( checkStrEquals(reverse)) {
System.out.println("the string is palindrome!");
return true;
}
else {
System.out.println("the string is not palindrome!");
return false;
}
}
else
return false;
}

public static void main(String argv[]) {
String inputString = "AAABBBBBBAAA";
CheckStringPalindrome csp = new CheckStringPalindrome(inputString);
csp.checkStrPalinddrome();
}
}

Fix version 1:

private String reverseStr() {
int length = original.length();
StringBuilder reverse= new StringBuilder(length);
for( int iCnt=0; iCnt < length; iCnt++ ) {
reverse.append(original.charAt(length-iCnt-1));
}
return reverse.toString();
}

问题这里是:stringBuffer has a reverse method.
fix 2:
private String reverseStr() {
StringBuilder temp = new StringBuilder(original);
return temp.reverse().toString();
}


Don't use a String if you want something mutable. Instead you're better off
using a StringBuilder object. Then when you're done modifying it, you can
call toString() on it to get the String you need. The API will show you the
methods of this flexible class.

String is immutable class. Here is another solution : convert to StringBuffer
or StringBuilder and delete from the startIndex to endIndex

String s = "test tes te t ";
String str = new StringBuffer(s).delete(start, end).toString();

for example :
String str = new StringBuffer(s).delete(0,.toString();
System.out.println(str); // s te t 
分享到:
评论

相关推荐

    CGI Programming with Perl

    在描述中提到的文件列表中,`[大家网]CGI Programming with Perl[www.TopSage.com].mobi`是一本关于Perl CGI编程的电子书,可能涵盖了Perl CGI的基本概念、实例演示以及高级技巧,对于学习和理解Perl CGI编程非常有...

    Get Programming - Learn to code with Python.epub

    Lesson 2 - Basic principles of learning a programming language UNIT 1 - VARIABLES, TYPES, EXPRESSIONS, AND STATEMENTS Lesson 3 - Introducing Python: a programming language Lesson 4 - Variables and ...

    Excel 2010 Programming with VBA

    《Excel 2010 Programming with VBA》是关于利用Visual Basic for Applications(VBA)在Excel 2010中进行编程的一本资源。VBA是Microsoft Office套件中的内置编程语言,允许用户自定义功能,自动化任务,以及创建...

    Apress.Web.Programming.with.Dart.2014

    ### Apress.Web.Programming.with.Dart.2014 知识点概览 #### 一、导言与概述 本书旨在为读者提供一个全面学习 Dart 编程语言的平台,帮助开发者掌握从基础到高级的 Dart 开发技能。Dart 是一种由 Google 设计的高...

    Java Programming With Oracle Sqlj

    Java Programming With Oracle Sqlj。 &lt;br&gt;详细说明,介绍。通后学习如何用 SQLJ 取代 JDBC 开发模式。 例子说明: int v_id = 1; String v_name = null; float v_price = 0.0; PreparedStatement...

    Win32 API Programming with Visual Basic|推荐代码

    在"Win32 API Programming with Visual Basic"这个资源中,我们可以学习如何在VB环境中调用Windows API函数,这通常涉及到以下关键知识点: 1. **API函数声明**:在VB中使用API前,必须先声明API函数。这通常通过`...

    Excel 2010 Power Programming with VBA

    ### Excel 2010 Power Programming with VBA #### 核心知识点概览 - **VBA基础**:包括VBA编程环境、变量与数据类型、控制结构等。 - **Excel对象模型**:涵盖工作簿、工作表、单元格、范围等对象的操作。 - **宏与...

    Hands-On Go Programming

    You will discover how to perform an arithmetic operation date and time, along with parsing them from string values. In addition to this, you will cover concurrency in Go, performing various web ...

    Parallel Programming with Spark

    - `def announce(text: String) { println(text) }` #### 八、总结 Spark 作为一种高性能的并行计算框架,凭借其优秀的内存计算能力和灵活的编程接口,在大数据处理领域占据了重要的地位。无论是本地部署还是云...

    LDAP Programming with Java

    public static void main(String[] args) { try { // 初始化上下文 Properties env = new Properties(); env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory"); env.put(Context....

    Get Programming with Go的源代码清单是Go编程语言的初学者指南.zip

    《Get Programming with Go》是一本面向初学者的Go编程语言指南,它的源代码清单提供了丰富的实践案例,帮助读者深入理解Go语言的基础概念和语法。在学习Go语言的过程中,掌握这些源代码将对初学者的技能提升...

    lcs.rar_Programming with C

    #include &lt;string.h&gt; int lcs(char *str1, char *str2, int m, int n) { // 初始化dp数组 // ... // 填充dp数组 // ... // 回溯找到LCS // ... } int main() { char str1[] = "ABCBDAB"; char str2[] = ...

    Win32 API Programming with Visual Basic, Fourth Edition 随书光盘

    **Win32 API Programming with Visual Basic, Fourth Edition 随书光盘** 这本书的随书光盘主要聚焦于使用Visual Basic进行Win32 API编程,它涵盖了如何利用API函数来扩展VB应用程序的功能。API(Application ...

    Modern C++ Programming Cookbook

    Over 100 recipes to help you overcome your difficulties with C++ programming and gain a deeper understanding of the working of modern C++ About This Book Explore the most important language and ...

Global site tag (gtag.js) - Google Analytics