十年网站开发经验 + 多家企业客户 + 靠谱的建站团队
量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决
//----------------先写了个,功能是实现了,有待改进-----------
10年积累的成都网站设计、网站建设经验,可以快速应对客户对网站的新想法和需求。提供各种问题对应的解决方案。让选择我们的客户得到更好、更有力的网络服务。我虽然不认识你,你也不认识我。但先做网站设计后付款的网站建设流程,更有温州免费网站建设让你可以放心的选择与我们合作。
import java.util.ArrayList;
import java.util.List;
/**
* 现有ListSourceBean类型的List对象,
* 现在需要将该List对象中a是奇数或者b100的对象过滤掉,
* 请用责任链模式实现此段代码。
* @author Administrator
*
*/
public class Chain_test1 {
public static void main(String[] args) {
ListSourceBean list = new ArrayListSourceBean();
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仅仅过滤b100
//这里对奇数过滤两次,
//即,过滤掉b100和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(ListSourceBean list){
for(SourceBean s: list){
System.out.print(s.toString());
}
}
}
class Filter{
private Filter filter;
public void doFilter(ListSourceBean list){
if (filter != null){
filter.doFilter(list);
}else{
System.out.println("处理结束");
}
}
public void setFilter(Filter filter){
this.filter = filter;
}
}
/**
* 仅仅过滤b100
*/
class CompareFilter extends Filter{
public void doFilter(ListSourceBean list){
for(int i= 0; i list.size(); i++){
if(list.get(i).getB() 100 ){//b100
list.remove(i);//移除该对象
}else{//交给其他Filter处理
super.doFilter(list);
}
}
}
}
/**
* 仅过滤a为奇数
*/
class OddFilter extends Filter{
public void doFilter(ListSourceBean 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]
这是我写的一个差不多,你看一下吧:
package com.test.list;
import java.io.BufferedReader;
import java.io.InputStreamReader;
public class LinkedList {
public static void main(String[] args) {
MyList l = new MyList();
MyListNode node = l.createList();
l.printNode(node);
//l.searchNode(node, 4);
//node = l.insertNode(node, 3, "g");
//l.printNode(node);
node = l.deleteNode(node, "d");
l.printNode(node);
}
}
class MyListNode {
public String data;
public MyListNode nextNode;
}
class MyList {
public MyListNode createList() {
MyListNode node = new MyListNode();
MyListNode q ,p;
q = new MyListNode();
q = node;
while (true) {
String s = null;
try {
BufferedReader br = new BufferedReader(new InputStreamReader(
System.in));
System.out.println("请输入节点数据:");
s = br.readLine();
if (s.equals("0")) {
break;
} else {
p = new MyListNode();
p.data = s;
p.nextNode = null;
q.nextNode = p;
q = p;
}
} catch (Exception e) {
e.printStackTrace();
}
}
return node;
}
public void printNode(MyListNode node) {
MyListNode p = node.nextNode;
while (p!= null) {
System.out.print(" "+p.data);
p = p.nextNode;
}
}
public void searchNode(MyListNode node, int i){
MyListNode p = node.nextNode;
int j = 1;
while (p != null ji) {
p = p.nextNode;
j++;
}
if( p == null || ji) {
System.out.println("error");
}
System.out.println(" --"+p.data+"--");
}
public MyListNode insertNode(MyListNode node, int i ,String s) {
MyListNode p = node.nextNode;
int j = 1;
while (p != null ji-1) {
p = p.nextNode;
j++;
}
if( p == null || ji-1) {
System.out.println("error");
}
MyListNode n = new MyListNode();
n.data = s;
n.nextNode = p.nextNode;
p.nextNode = n;
return node;
}
public MyListNode deleteNode(MyListNode node ,String s) {
MyListNode p = node;
while(p.nextNode != null !p.nextNode.data.equals(s)) {
p = p.nextNode;
}
p.nextNode = p.nextNode.nextNode;
return node;
}
}
/*逆位序创建
public MyListNode createList() {
MyListNode node = new MyListNode();
node.nextNode = null;
while(true) {
String s = null;
try {
BufferedReader br = new BufferedReader(new InputStreamReader(
System.in));
System.out.println("请输入节点数据:");
s = br.readLine();
if(s.equals("0")) {
break;
}else {
MyListNode n = new MyListNode();
n.data = s;
n.nextNode = node.nextNode;
node.nextNode = n;
}
} catch (Exception e) {
e.printStackTrace();
}
}
return node;
}
*/
建议可以用第三方,你参考下spire.doc for java的超链接设置方法,代码方案:Java 添加超链接到Word文档,包括了给文字添加链接,给图片添加链接,添加网页链接、邮箱链接、文档链接不同类型的链接。
a href="your target url"Link/a
如果在servlet中:
PrintWriter out = res.getWriter();
out.println("a href='your target url'Link/a");
如果是连接自己项目中的,请注意相对路径的写法。建议加上context path.