将给定的一字符串倒序输出,用 JAVA
一、利用String类的toCharArray(),再倒序输出数组。
示例代码:
import javax.swing.JOptionPane;
public class ReverseString {
public static void main (String args[]){
String originalString;
String resultString = ;
originalString = JOptionPane.showInputDialog(Please input a String: );
char[] charArray = originalString.toCharArray();
for (int i=charArray.length-1; i>=0; i--){
resultString += charArray[i];
}
JOptionPane.showMessageDialog(null, resultString, Reverse String, JOptionPane.INFORMATION_MESSAGE);
}
}
二、利用String类提供的subString()方法,利用递归的办法输出倒序字符串。
示例代码:
import javax.swing.JOptionPane;
public class ReverseString {
public static void reverseString (String str){
if (str.length() == 1){
System.out.print(str);
}else{
String subString1 = str.substring(0, str.length()-1);
String subString2 = str.substring(str.length()-1);
System.out.print(subString2);
reverseString (subString1);
}
}
public static void main (String args[]){
String originalString;
originalString = JOptionPane.showInputDialog(Please input a String: );
reverseString (originalString);
}
}
三、把字符串定义成StringBuffer类,用StringBuffer类中的reverse()方法直接倒序字符串。
示例代码:
import javax.swing.JOptionPane;
public class ReverseString {
public static void reverseString (String str){
StringBuffer stringBuffer = new StringBuffer (str);
System.out.print(stringBuffer.reverse());
}
public static void main (String args[]){
String originalString;
originalString = JOptionPane.showInputDialog(Please input a String: );
reverseString (originalString);
}
}
用java代码实现责任链模式的骨干代码
//----------------先写了个,功能是实现了,有待改进-----------
import java.util.ArrayList;
import java.util.List;
/**
* 现有List<SourceBean>类型的List对象,
* 现在需要将该List对象中a是奇数或者b>100的对象过滤掉,
* 请用责任链模式实现此段代码。
* @author Administrator
*
*/
public class Chain_test1 {
public static void main(String[] args) {
List<SourceBean> list = new ArrayList<SourceBean>();
SourceBean bean1 = new SourceBean(1, 100);
SourceBean bean2 = new SourceBean(2, 200);
SourceBean bean3 = new SourceBean(3, 100);
SourceBean bean4 = new SourceBean(4, 100);
SourceBean bean5 = new SourceBean(5, 20);
SourceBean bean6 = new SourceBean(6, 200);
list.add(bean1);
list.add(bean2);
list.add(bean3);
list.add(bean4);
list.add(bean5);
list.add(bean6);
System.out.println(过滤前List中的内容);
printList(list);
// 装配 链条
Filter oddFilter = new OddFilter();
//为了保持Filter里面的逻辑不混乱,OddFilter仅过滤奇数,CompareFilter仅仅过滤b>100
//这里对奇数过滤两次,
//即,过滤掉b>100和a是奇数 共存的情形
Filter oddFilter2 = new OddFilter();
Filter compareFilter = new CompareFilter();
oddFilter.setFilter(null);
compareFilter.setFilter(oddFilter);
oddFilter2.setFilter(compareFilter);
// 进行过滤
oddFilter2.doFilter(list);
System.out.println(\n过滤后List中的内容);
printList(list);
}
public static void printList(List<SourceBean> list){
for(SourceBean s: list){
System.out.print(s.toString());
}
}
}
class Filter{
private Filter filter;
public void doFilter(List<SourceBean> list){
if (filter != null){
filter.doFilter(list);
}else{
System.out.println(处理结束);
}
}
public void setFilter(Filter filter){
this.filter = filter;
}
}
/**
* 仅仅过滤b>100
*/
class CompareFilter extends Filter{
public void doFilter(List<SourceBean> list){
for(int i= 0; i< list.size(); i++){
if(list.get(i).getB() > 100 ){//b>100
list.remove(i);//移除该对象
}else{//交给其他Filter处理
super.doFilter(list);
}
}
}
}
/**
* 仅过滤a为奇数
*/
class OddFilter extends Filter{
public void doFilter(List<SourceBean> list){
for(int i= 0; i< list.size(); i++){
if(list.get(i).getA() % 2 != 0){//非偶数
list.remove(i);//移除该对象
}else{//交给其他Filter处理
super.doFilter(list);
}
}
}
}
class SourceBean {
private int a;
private int b;
public SourceBean(int a, int b){
this.a = a;
this.b = b;
}
public SourceBean(){
}
public String toString(){
return [a=+a+, b=+b+] ;
}
public int getA() {
return a;
}
public void setA(int a) {
this.a = a;
}
public void setB(int b) {
this.b = b;
}
public int getB() {
return b;
}
}
//------------------------执行结果----------------------------
过滤前List中的内容
[a=1, b=100] [a=2, b=200] [a=3, b=100] [a=4, b=100] [a=5, b=20] [a=6, b=200]
过滤后List中的内容
[a=4, b=100]
- 相关评论
- 我要评论
-