- 浏览: 80711 次
文章分类
- 全部博客 (89)
- web service (9)
- subversion (1)
- JBOSS (3)
- interview (23)
- jQery (2)
- ExtJs (0)
- Axis (0)
- Design pattern (3)
- Agile (2)
- mutithread (0)
- Core Java (24)
- programming methods (1)
- SSH (7)
- jee design (1)
- OO (4)
- books (8)
- other (1)
- JSF (7)
- seam (2)
- Weblogic (4)
- JPA (1)
- ADF (1)
- Spring (5)
- Tomcat (1)
- DWR (2)
- JEE (3)
- Servlet (1)
- EJB (1)
- JDBC (3)
最新评论
-
iloveflower:
呵呵。好好学习。。。。。。。。。。。。
java 读书 -
Eric.Yan:
看了一点,不过是电子版的……你这一说到提醒我了,还要继续学习哈 ...
java 读书
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
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
发表评论
-
Java Collection summary
2012-06-16 02:40 577Collection:List、Set Map: ... -
When to use Comparable vs Comparator
2012-06-15 00:52 792I have a list of objects I need ... -
Arrays.fill with multidimensional array in Java
2012-06-15 00:09 696How can I fill a multidimension ... -
Immutable objects
2012-06-14 23:49 720Immutable objects are simply ... -
Implementing hashCode; Transaction.java
2012-06-14 23:43 826Below is the syntax highlight ... -
Lazy initialization
2012-06-14 22:48 802http://www.javapractices.com/to ... -
How to sort an array,mid of linkedlist, reverse int
2012-06-13 07:47 939A common mistake for a beginner ... -
Java各类型转换
2012-06-13 05:25 705各种数字类型转换成字符串型: String s = Str ... -
regular expression
2012-06-13 03:08 5201、Java对反斜线处理的 ... -
string functions
2012-06-13 00:09 853import java.util.*; public c ... -
String array to arraylist
2012-06-13 00:07 587There are some important thing ... -
core java interview summary
2012-06-12 04:11 385http://blog.sina.com.cn/s/blog_ ... -
OO Design books -good website
2012-06-07 03:13 700I’m always on the search on goo ... -
Write a String Reverser (and use Recursion!)
2012-06-07 03:03 524Java Interview questions: Write ... -
Java高手必会的要点
2012-05-29 03:28 611http://developer.51cto.com/art/ ... -
How to use getClass().getClassLoader().getResource()
2012-05-29 03:13 1756This is the simplest wat to get ... -
How to override equals method in Java
2012-05-12 02:57 1542Object class holds some very in ... -
Top 30 Programming interview questions
2012-05-12 02:48 910Programming questions are integ ... -
10 example of using ArrayList in Java >>> Java ArrayList Tutorial
2012-05-12 02:37 881ArrayList in Java is most frequ ... -
How to use Comparator and Comparable in Java? With example
2012-05-12 02:21 769Read more: http://javarevisited ...
相关推荐
在描述中提到的文件列表中,`[大家网]CGI Programming with Perl[www.TopSage.com].mobi`是一本关于Perl CGI编程的电子书,可能涵盖了Perl CGI的基本概念、实例演示以及高级技巧,对于学习和理解Perl CGI编程非常有...
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》是关于利用Visual Basic for Applications(VBA)在Excel 2010中进行编程的一本资源。VBA是Microsoft Office套件中的内置编程语言,允许用户自定义功能,自动化任务,以及创建...
### Apress.Web.Programming.with.Dart.2014 知识点概览 #### 一、导言与概述 本书旨在为读者提供一个全面学习 Dart 编程语言的平台,帮助开发者掌握从基础到高级的 Dart 开发技能。Dart 是一种由 Google 设计的高...
Java Programming With Oracle Sqlj。 <br>详细说明,介绍。通后学习如何用 SQLJ 取代 JDBC 开发模式。 例子说明: int v_id = 1; String v_name = null; float v_price = 0.0; PreparedStatement...
在"Win32 API Programming with Visual Basic"这个资源中,我们可以学习如何在VB环境中调用Windows API函数,这通常涉及到以下关键知识点: 1. **API函数声明**:在VB中使用API前,必须先声明API函数。这通常通过`...
### Excel 2010 Power Programming with VBA #### 核心知识点概览 - **VBA基础**:包括VBA编程环境、变量与数据类型、控制结构等。 - **Excel对象模型**:涵盖工作簿、工作表、单元格、范围等对象的操作。 - **宏与...
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 ...
- `def announce(text: String) { println(text) }` #### 八、总结 Spark 作为一种高性能的并行计算框架,凭借其优秀的内存计算能力和灵活的编程接口,在大数据处理领域占据了重要的地位。无论是本地部署还是云...
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编程语言指南,它的源代码清单提供了丰富的实践案例,帮助读者深入理解Go语言的基础概念和语法。在学习Go语言的过程中,掌握这些源代码将对初学者的技能提升...
#include <string.h> 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 随书光盘** 这本书的随书光盘主要聚焦于使用Visual Basic进行Win32 API编程,它涵盖了如何利用API函数来扩展VB应用程序的功能。API(Application ...
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 ...