最近在看《大话数据结构》这本书,真是受益匪浅,此处分享一下书中提到的冒泡排序算法的优化:
public class BubbleSort {
public static void main(String[] args) {
int[] arr = {1,3,2,9,10,5,1,8,4};
boolean flag = true;
for(int i = 0; i<arr.length && flag; i++){
System.out.println("i=" + i);
flag = false;
for(int j=arr.length-2; j >= i; j--){
if(arr[j] > arr[j+1]){
int temp = arr[j];
arr[j]= arr[j+1];
arr[j+1] = temp;
flag = true;
}
}
}
for(int i : arr){
System.out.print(i);
System.out.print(" ");
}
}
}
打印结构:
i=1
i=2
i=3
i=4
1 1 2 3 4 5 8 9 10
经过优化后此例中整个外层循环只循环了5次就到达了目的;
已有 0 人发表留言,猛击->>这里<<-参与讨论
ITeye推荐
- —软件人才免语言低担保 赴美带薪读研!—
oyhk学习笔记
elasticsearch简称ES
jest
好吧下面我介绍下jest(第三方工具),个人认为还是非常不错的...想对ES用来更好,多多研究源代码吧...迟点,会写一些关于ES的源代码研究文章,现在暂时还是入门的阶段.哈..(不敢,不敢)
- 它是ES的java客户端,基于http restful...
- jest是开源的....其他就不清楚了,看源代码吧..哈.
如果对ES不了解请看:elasticsearch RESTful搜索引擎-简介
上一篇文章:elasticsearch RESTful搜索引擎-安装
费话不多说了,下面开始 ES -->> jest 入门
首先看看项目的目录结构
我一般习惯了用maven去管理我的项目...所以...看pom.xml吧
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.mkfree</groupId> <artifactId>ES-jest</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>war</packaging> <dependencies> <!-- jest --> <dependency> <groupId>io.searchbox</groupId> <artifactId>jest</artifactId> <version>0.0.2</version> </dependency> <!-- elasticsearch --> <dependency> <groupId>org.elasticsearch</groupId> <artifactId>elasticsearch</artifactId> <version>0.20.2</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.10</version> </dependency> </dependencies> <repositories> <!-- 添加 sonatype仓库--> <repository> <id>sonatype</id> <name>Sonatype Groups</name> <url>https://oss.sonatype.org/content/groups/public/</url> </repository> </repositories> </project>
1.配置jest客户端
InitES类
package com.mkfree.jest.config;
import io.searchbox.client.JestClient;
import io.searchbox.client.JestClientFactory;
import io.searchbox.client.config.ClientConfig;
import io.searchbox.client.config.ClientConstants;
import java.util.LinkedHashSet;
/**
* 初始化连接es服务端,这里相当于dao层..自己去理解吧..
*
* @author hk
*
* 2013-1-12 下午11:27:37
*/
public class InitES {
/**
* 静态,单例...
*/
private static JestClient JestClient;
/**
* 配置jest客户端,到时使用spring时,可以用配置方式 ,现在暂时使用new ...
*
* @return
*/
private static ClientConfig clientConfig() {
// es的服务端地址,暂时我是用我虚拟机的(ubuntu)做服务器
String connectionUrl = "http://192.168.56.101:9200";// 一般都是9200端口
ClientConfig clientConfig = new ClientConfig();
// 当你用集群时,就有可能会有多个es的服务端,这里我暂时没有集群
LinkedHashSetservers = new LinkedHashSet();
servers.add(connectionUrl);
clientConfig.getServerProperties().put(ClientConstants.SERVER_LIST, servers);
clientConfig.getClientFeatures().put(ClientConstants.IS_MULTI_THREADED, false);
return clientConfig;
}
/**
* 获取一个jest的对象
*
* @return
*/
public static JestClient jestClient() {
JestClientFactory factory = new JestClientFactory();
factory.setClientConfig(clientConfig());
if (JestClient != null) {
JestClient = factory.getObject();
}
return JestClient;
}
}
News 新闻类
package com.mkfree.jest.domain;
import io.searchbox.annotations.JestId;
/**
* 虚拟news 搜索文章
*
* @author hk
*
* 2013-1-12 下午11:38:29
*/
public class News {
@JestId
private int id;
private String title;
private String content;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
}
SearchService 搜索服务接口
package com.mkfree.jest.service;
import io.searchbox.client.JestClient;
import io.searchbox.client.JestResult;
import io.searchbox.core.Bulk;
import io.searchbox.core.Index;
import io.searchbox.core.Search;
import io.searchbox.indices.CreateIndex;
import io.searchbox.indices.DeleteIndex;
import java.io.IOException;
import java.util.List;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import com.mkfree.jest.config.InitES;
import com.mkfree.jest.domain.News;
/**
* es简单服务接口
*
* @author hk
*
* 2013-1-12 下午11:47:16
*/
public class SearchService {
private static JestClient jestClient = InitES.jestClient();
/**
* 创建es news索引
*/
public void builderSearchIndex() {
int num = 10000;
long start = System.currentTimeMillis();
try {
// 如果索引存在,删除索引
DeleteIndex deleteIndex = new DeleteIndex("news");
jestClient.execute(deleteIndex);
// 创建索引
CreateIndex createIndex = new CreateIndex("news");
jestClient.execute(createIndex);
// Bulk 两个参数1:索引名称2:类型名称(用文章(article)做类型名称)
Bulk bulk = new Bulk("news", "article");
// 添加添加100万条假数据去服务端(ES)
for (int i = 0; i < num; i++) {
News news = new News();
news.setId(i + 1);
news.setTitle("elasticsearch RESTful搜索引擎-(java jest 使用[入门])" + (i + 1));
news.setContent("好吧下面我介绍下jest(第三方工具),个人认为还是非常不错的...想对ES用来更好,多多研究源代码吧...迟点,会写一些关于ES的源代码研究文章,现在暂时还是入门的阶段.哈..(不敢,不敢)"
+ (i + 1));
bulk.addIndex(new Index.Builder(news).build());
}
jestClient.execute(bulk);
} catch (Exception e) {
e.printStackTrace();
}
long end = System.currentTimeMillis();
System.out.println("创建索引时间:数据量是 " + num + "记录,共用时间 -->> " + (end - start) + " 毫秒");
}
/**
* 搜索新闻
*
* @param param
* @return
*/
public ListsearchsNews(String param) {
try {
long start = System.currentTimeMillis();
QueryBuilder queryBuilder = QueryBuilders.queryString(param);
Search search = new Search(Search.createQueryWithBuilder(queryBuilder.toString()));
search.addIndex("news");
search.addType("article");
JestResult result = jestClient.execute(search);
long end = System.currentTimeMillis();
System.out.println("在100万条记录中,搜索新闻,共用时间 -->> " + (end - start) + " 毫秒");
return result.getSourceAsObjectList(News.class);
} catch (IO
我们在用Myeclipse进行开发的时候会用到很多插件,比如jad、svn、properties等
安装方法:
方法一、如果可以上网可在线安装
1. 打开Myeclipse,在菜单栏中选择Help→Software Updates→Find and Install;
2. 选择Search for new features to install,点击Next进入下一步;
3. 点击"New Remote Site"按钮,在弹出的对话框中输入:
name:SVN
url: http://subclipse.tigris.org/update_1.4.x
点击OK,关闭对话框,并点击Finish按钮,Myeclipse自动下载插件安装程序;
4. 下载完插件之后,进入安装画面。
5. 选择所要安装的SVN插件内容,这里去掉第二个选项Subclipse Integrations,点击下一步;
6. 选择 "I accept the terms in the license agreements"并点击Next,直到点击Finish即可,进入下一步。
7. 开始安装SVN插件,安装完成之后,重启Myeclipse。
方法二、无法连接网络的情况
1、下载SVN插件
下载地址:
http://subclipse.tigris.org/files/documents/906/46495/site-1.6.5.zip
2. 在MyEclipse 6.5的安装路径下的plug_in(C:/Program Files/MyEclipse 6.5/eclipse/plugins)下新建文件夹:site-1.6.5;
3. 打开MyEclipse 6.5,在菜单栏中选择 Help → Software Updates → Find and Install;
2. 选择Search for new features to install,点击Next进入下一步;
3. 点击"New Local Site"按钮,找到解压出来的文件夹(site-1.6.5) → 点击OK,并点击Finish按钮,Myeclipse自动加载插件;
4. 在弹出的菜单中,select the features to insteall:在刚加进去的路径Subclipse-site-1.6.5这一项前打钩,此时会报错,然后展开Subclipse前的加号,去掉Subclipse Integration for Mylyn 3.x(Optional)3.0.0前的钩,错误消失 → next → 选 I accept the terms in the license agreements → next → next → 选中所有的8项,然后Change Location……,在弹出的菜单中选Add Location……选择在第3步中建的文件夹的路径(C:/Program Files/MyEclipse 6.5/eclipse/plugins/site-1.6.5) → Finish → 再弹出的菜单中选 Install All → Yes
5. 开始安装SVN插件,安装完成之后,重启Myeclipse。
方法三、无法连接网络的情况(link链接)
1、从官网下载 Site-1.6.5.zip
2、解压Site-1.6.5.zip至${eclipse}/Plugins/Subclipse-1.6.5.zip,并删除 site.xml(很重要)(即把Site-1.6.5.zip解压出来的features和plugins文件夹里所有的jar包和两个XML文件artifacts.xml、content.xml添加并替换到plugins文件夹下。例如:我应添加在C:/Program Files/MyEclipse 6.5/eclipse/plugins/目录下)
3、创建link文件 ${eclipse}/linksSubclipse-1.6.5.zip.link (即在link文件夹下添加linksSubclipse-1.6.5.link文件)其内容为: path=C:/Program Files/MyEclipse 6.5/eclipse/
4、重启Eclipse就是把subclipse集成完毕
二、安装完成
1. 在MyEclipse 6.5 菜单栏中选择Window→Open Perspective→Other打开Myeclipse试图列表。这个时候Myeclipse的视图列表中,就出现了"SVN Repository Exploring"一项。
2. 打开"SVN Repository Exploring"视图。在左边空白区域,单击右键 → New → Repository Location。
3. 在Url一栏中输入svn://IP,点击Finish按钮。
4. 选择要下载的项目右键选择checkout 就把项目下载到本地了
三、基本操作
1.提交工程
右击工程->小组->共享项目->选择SVN->选择svn://127.0.0.1(如果没有,则创建一个新的资源库)->下一步->完成
2.下载工程
在SVN资源库透视图下,点开svn://127.0.0.1,会显示出现在本机SVN上的所有工程,右击你想下载的工程->检出为->下一步->完成
3.操作工程
3.1同步
在MyEclipse J2EE透视图下,右击你要同步的工程->小组->与资源库同步->这时会进入同步透视图,会显示出本机与SVN上内容有不同的文件,双击文件名,会显示出两个文件中哪里不同;
3.2提交
在同步透视图下有"灰色向右的箭头,表示你本机修改过",右击该文件,可以选择提交操作;
3.3覆盖/更新
在同步透视图下有"蓝色向左的箭头,表示你本机修改过",右击该文件,可以选择覆盖/更新操作;
4.图标说明
4.1灰色向右箭头:本地修改过
4.2蓝色向左箭头:SVN上修改过
4.3灰色向右且中间有个加号的箭头:本地比SVN上多出的文件
4.4蓝色向左且中间有个加号的箭头:SVN上比本地多出的文件
4.5灰色向右且中间有个减号的箭头:本地删除了,而SVN上未删除的文件
4.6蓝色向左且中间有个减号的箭头:SVN上删除了,而本地未删除的文件
4.7红色双向箭头:SVN上修改过,本地也修改过的文件
5.一些我遇到的出错信息
5.1在上面讲的3.2步输入URL(svn://127.0.0.1)点下一步出现"svnserve.conf:12: Option expected"错误
你打开svnserve.conf文件中的第12行,该错误是由于该行的前面有空格引起的,把左边多出的空格删除掉即可;
5.2在上面讲的3.2步输入URL(svn://127.0.0.1/SVN_PRJ)点下一步出现"svn://127.0.0.1/SVN_PRJ non-existent in revision '7'"错误
URL错了,应该输入svn://127.0.0.1即可