博文

目前显示的是 2016的博文

String.replace()

  今天想用String的replace()做替换,但是怎么也得不到想要的结果:   grammarData.replace('n', '\n');   困扰了好一阵子,最后只好把替换前后打印了出来,才恍然大悟:忘了String是不可变的了!于是加了个变量,搞定:   strGrammarData = grammarData.replace('n', '\n');   说起String的特征都明白,但是一coding就给忘了 -_-b

arrayList.toArray()的用法

 做一个arrayList<String>到String[]的转换,调用toArray(),编译出错:     String[] sTemp = (String[])alTemp.toArray();  查了一下,原来toArray有两种方法:toArray()和toArray(T[] a),前者返回object,后者返回指定的<T> T[]  所以想得到String[]的转换,应该调用第二种方法:     String[] sTemp = (String[])alTemp.toArray(new String[]{});

后知后觉了,原来java7开始就支持用string作为switch语句的条件了

public class SwitchString {     public static void test(String str) {         switch(str) {         case "abc":             System.out.println("abc");             break;         case "def":             System.out.println("def");             break;         default:             System.out.println("default");         }     } public static void main(String[] args) { test("def"); } }

Windows平台上Visual Studio C/C++内存泄漏自动检查的工具VLD

VLD是一个用于Windows平台上Visual Studio C/C++内存泄漏自动检查的工具。 VLD通过对内存分配过程进行动态跟踪,在每次内存分配的时候记录其上下文,当程序退出时对检测到的内存泄漏查找其上下文信息,并转换成报告输出到Output中 VLD只能用于Windows平台下,不能用于Linux。 Linux平台下的内存泄漏检测,可以使用 Valgrind 1. 安装VLD 安装前请关闭Visual Stuido 可到VLD的官网下载: http://vld.codeplex.com/ 安装过程中全部接受默认选项与设置即可 2. 打开Visual Studio,检查一下VLD是否已经正确关联: 工具->选项->项目和解决方案->VC++目录->包含文件:确认VLD目录已在,默认安装路径是 C:\Promgram Files(x86)\Visual Memory Detector\include 工具->选项->项目和解决方案->VC++目录->库文件:确认VLD目录已在,默认安装路径是 C:\Promgram Files(x86)\Visual Memory Detector\lib\win32 3. 使用时,在包含入口函数的.cpp文件中包含vld.h就可以。 如果这个cpp文件中包含了预编译头文件stdafx.h,则将包含vld.h的语句放在stdafx.h的包含语句之后,否则放在最前面。 4. 创建一个试验项目: #include<vld.h>    //在文件的最前面包含VLD的头文件 #include<stdlib.h> #include<stdio.h> void f() {     int *p = new int(0x12345678);   //这里new了一个int,4个字节     printf("p=%08x, ", p); } int main() {     f();     return 0; } 5. 编译 debug 模式并执行(注意不要编译成发布release版本),...

Windows上调试C/C++程序时自动产生coredump的设置方法

1. 安装Windows debug tool,全部使用默认安装选项即可 搜索 Windbgx86_v6.12.2.633.1395371577.msi 并下载 或者也可到微软官网上下载windows调试工具,windbg也在其中: https://developer.microsoft.com/zh-cn/windows/hardware/download-windbg 注:此版本为32位版,但在32位和64位操作系统上均能使用 2. 将windbg.exe设置为默认debugger工具: 起一个cmd,进入windbg的安装目录,默认是C:\Program Files (x86)\Debugging Tools for Windows (x86)\ 然后执行C:> windbg.exe -I 注意-I必须大写 然后windbg的程序主窗口会自动打开,并且弹出一条信息windbg已经被成功设置为默认的debugger云云... 点确定后窗口会自动关闭,至此设置成功 3. 创建一个存放coredump文件的目录 D:\Dump 4. 修改系统注册表,捕获异常崩溃时自动产生Coredump文件:   1) regedit打开注册表,找到注册表项,先备份:      64位系统: HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\Windows NT\CurrentVersion\AeDebug      32位系统: HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\AeDebug   2) 修改Auto的值,原来的默认值为0不启动debugger,修改为1默认启动debugger   3) 修改Debugger的值,如果上述第2步执行成功,那么此处的值应该已经被改为了"C:\Program Files (x86)\Debugging Tools for Windows (x86)\windbg.exe" -p %ld -e %ld -g      现在需要将其修改为导出并保存co...

a Windows PowerShell Script used to monitor an Android process' CPU/Mem info

for used on Android 不过Android各个版本之间格式输出差异较大,需要依据具体格式调整 脚本可监控Android平台上某个应用的系统资源使用率,包括CPU,物理内存PSS,总堆内存Heap,Dalvik虚拟机堆内存。Heap和Dalvik之差即为jni的堆内存消耗。 使用之前先保证PC上adb环境可用,Android手机的驱动也安装了 然后用adb shell ps找到需要监控的应用进程,及对应的进程号 processID # define the process id that want to be monitored $processId = "278" # define the time interval for data collection, in seconds. $timeInterval = 2 # define the continuous running hours. # generally should be 24, that's for 1 day; or 72, for 3 days # if need to take a test, set it as 5/3600, which means 5 seconds $rHours = 2/3600 # define output data folder path, note there must be a "\" after disk & path $disk = "C:\" $path = "logs\" $folder = $disk+$path # DO NOT CHANGE BELOW IF NO NEED # create output data folder if not exist if (test-path $folder){echo "$folder already exit"} else {new-item -path $disk -name $path -type directory} # got output data file name $ymd = get-date -...

a Windows PowerShell Script used to monitor the CPU/Mem info of a specified process

copy the contents below and save it as whateverName.ps1: # this Windows PowerShell Script is used to monitor the CPU/Mem info of a specified process # there will be a temporary .csv file during running, don't change/remove it before this script complete # refer to .log for the final data after running # by cbzhan@gmail.com, 2016-12-1 # if complained no privilege when execute, run this command in PowerShell Console: Set-ExecutionPolicy Unrestricted # define the process name that want to be monitored, which can be got from Windows' taskmgr, and no need to include file suffix, e.g. .exe $psName = "taskmgr" # define the time interval for data collection, in seconds. generally 30 secondes will be good enough $timeInterval = 2 # define the continuous running hours. if need input minutes, e.g. 5 minutes, use format as 5/60. if need input seconds, e.g. 5 seconds, use format as 5/3600 $rHours = 10/3600 # define output data folder path, note there must be a ...

feed4junit-1.1.22报警告Warning: SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder"

  这里有个兼容性的问题,需手动添加slf4j-log4j12和log4j的lib库文件。以slf4j-log4j12-1.7.21和log4j-1.2.17为例: 1)先到slf4j和log4j的网站去下载安装包。 2)从slf4j的安装包中提取slf4j-log4j12-1.7.21.jar文件,从log4j的安装包中提取log4j-1.2.17.jar文件,并放入feed4junit的lib目录下。   这里slf4j-log4j12和log4j的版本应该关系不大,别的版本应该也可以。而且因为我是选择了slf4j-log4j12,所以后面才需要log4j,如果前面选的是别的日志系统,如NOP, Simple, jdk等,那么后面也应该选择对应的日志库。   SLF4j只是为各种loging APIs提供一个简单统一的接口,从而使得最终用户能够在部署的时候配置自己希望的loging APIs实现。包括直接实现SLF4J接口的loging APIs如: logback、SimpleLogger;还有通过开发相应的适配器来使用已有的API实现如Log4jLoggerAdapter、JDK14LoggerAdapte等等。换言之slf4j-api本身只是提供了一个日志接口,并没有日志的实现,所以如果没有具体实现的lib会报上面的警告,而这里做的就是给它加上log4j的实现。 3)在Eclipse里import,再在project->properties里Add JARs和feed4junit的libs一样加进来。 4)对log4j还需要增加一个property文件log4j.properties。创建一个log4j.properties文件,可以和class文件放在一个目录下,拷贝如下内容: # Set root logger level to DEBUG/WARN/ERROR/FATAL and its only appender to A1. log4j.rootLogger= DEBUG , A1 # A1 is set to be a ConsoleAppender. log4j.appender.A1=org.apache.log4j.ConsoleAppender # A1 uses...