程序是由数据和算法构成的,数据一般有内存数据,硬盘文件或者数据库。下面总结Java中常用的数据结构:
数组,数组元素可以是简单数据类型,也可以是符合数据类型。声明有两种格式。由于数组是定长的,所以在动态调正时可能需要使用System.arrayCopy功能。
集合类,ArrayList是一个Java的类,并不是语法的一部分。另外集合类只能包含对象引用,不能用简单类型如int等。同数组相比,集合类效率稍微低一些,但是并没有太过影响性能。同时,由于允许多线程同时访问独享,对于单项称应用程序而言,采用Arraylist,其效率会有提升。
ArrayList<String>alA = new ArrayList<String>();
alA.add("gongqingkui");
System.out.println(alA.get(0));
for (String s :alA) {
System.out.println(s);
}
System.out.println(alA.contains("gongqingkui"));
Iterator<String>i = alA.iterator();
while(i.hasNext()) {
System.out.println(i.next());
}
为了解决在不知道在集合类型的情况下查看集合元素的问题,我们需要定义元素实现一个Iterator接口。
使用HashMap和HashTable可以实现一组对象到另一组对象的单向映射表(也叫散列表)。这种散列关系是一种键-值对的关系。
HashMap<String,String>hm = new HashMap<String,String>();
hm.put("gongqingkui","shandongtengzhou");
hm.put("zhangsan","zhejiangzhuji");
hm.put("lisi","hebeizhengding");
Iterator hmi1 =hm.keySet().iterator();
while(hmi1.hasNext()) {
String s = (String) hmi1.next();
System.out.println(s+":"+hm.get(s));
}
使用Preferences类可以实现与操作系统相关的属性存取任务。Properties和HashMap比较相似,实际上他是由后者继承而来的。用于读取配置文件的信息,同时注意配置文件采用的是key-value格式。
Preferences p =Preferences.userNodeForPackage(p7structData.class);
String text =p.get("textFont", "bold");
System.out.println(text);
p.put("textFont","Itaic");
Properties pro =new Properties();
try {
pro.load(newFileReader("a.txt"));
pro.setProperty("name","gongqingkui");
System.out.println(pro.getProperty("name"));
System.out.println(pro.getProperty("age"));
pro.list(System.out);
} catch(FileNotFoundException e) {
e.printStackTrace();
} catch(IOException e) {
e.printStackTrace();
}
排序问题要针对不同对象,数组可以使用数组的sort方法,集合可以使用集合的sort方法,如果单独定义,则需要单独实现类对象的comparator方法。实现comparator接口,必须实现它的方法compare。
public class p7comparator implements Comparator<String>{
public int compare(String o1, String o2) {
return o1.compareTo(o2);
}
public static void main(String[] args) {
String [] ss=new String[]{"c","b","a"};
Arrays.sort(ss,new p7comparator());
System.out.println(Arrays.toString(ss));
}
}
为了避免重复性排序,可以使用TreeSet或者TreeMap对象来进行元素组织,这种对象可以保持元素始终有序。TreeSet实现元素有序,TreeMap实现主键有序。其方法headSet或者headMap返回前面的有序序列,tailSet和tailMap实现后面的有序序列。
TreeSet<String>ts = new TreeSet<String>();
ts.add("ccc");
ts.add("aaa");
ts.add("bbb");
ts.add("ddd");
ts.add("fff");
Iterator<String>tsi = ts.iterator();
while(tsi.hasNext()) {
System.out.println(tsi.next());
}
System.out.println(ts.headSet("ddd"));
System.out.println(ts.tailSet("ddd"));
输出为:aaa
bbb
ccc
ddd
fff
[aaa, bbb, ccc]
[ddd, fff]
HashSet可以实现元素的无重复值存放。
HashSet<String>hs = new HashSet<String>();
hs.add("a");
hs.add("a");
hs.add("b");
hs.add("b");
System.out.println(hs);
输出为:[b, a]
堆栈使用Java.util.Stack类来实现。
Stack<String>s = new Stack<String>();
s.push("1");
s.push("2");
s.push("3");
System.out.println(s.pop()+s.pop()+s.pop());
输出为:321
(作者:LL 出处:http://blog.csdn.net/tcpipstack , 欢迎转载,也请保留这段声明。谢谢!)
经过第二课 eLua学习第二课:在Ubuntu OS下的Lua源码安装方法 的学习,我们已经将Lua的开发环境安装好了,那么在这一课中我们将学习Lua的基本知识。
编程语言之于程序员,若武功招式之于习武之人,招式虽重要,但在于使用之人。胜者之道,武功只行于表,高手用剑,片草只叶亦威力无穷。
当今武林,派别林立,语言繁杂,林林总总不计其数。主流文化的C/C++、Java、C#、VB;偏安一隅的Fortran;动态语言中的Perl、Tcl、Ruby、Forth、Python,以及本书介绍的Lua;..,等等等等。
虽说语言的威力依使用者本身的修为高低而定,但不同语言本身的设计又有不同。若让用Java写写操作系统内核、Perl写写驱动程序、C/C++写写web应用,都无异于舍近求远,好刀只用上了刀背。
Lua本身是以简单优雅为本,着眼于处理那些C不擅长的任务。借助C/C++为其扩展,Lua可闪现无穷魅力。Lua本身完全遵循ANSI C而写成,只要有C编译器的地方,Lua便可发挥她的力量。Lua不需要追求Python那样的大而全的库,太多的累赘,反而会破坏她的优美。
(以上内容摘自《Lua中文教程》)
hello world作为所有编程语言的起始阶段,所有的编程第一步就在于此了!
我们先使用Lua语言来实现这一步:
root@long-desktop:/work/prjs/lua-5.2.1# lua
Lua 5.2.1 Copyright (C) 1994-2012 Lua.org, PUC-Rio
> print "Hello World"
Hello World
> io.write("Hello World\n")
Hello World
> 上面的代码展示了2种实现方法,io.write("Hello World\n") 更符合Lua的style,推荐使用。
在Lua中,一切都是变量,除了关键字。请记住这句话。
1. 标示符标示符:字母(letter)或者下划线开头的字母、下划线、数字序列.最好不要使用下划线加大写字母的标示符,因为Lua的保留字也是这样的。Lua中,letter的含义是依赖于本地环境的。
保留字:以下字符为Lua的保留字,不能当作标识符。
and break do else elseif end false for function if in local nil not or repeat return then true until while
3. 注释
写一个程序,总是少不了注释的。
在Lua中,你可以使用单行注释和多行注释。
单行注释中,连续两个减号"--"表示注释的开始,一直延续到行末为止。相当于C++语言中的"//"。
多行注释中,由"--[["表示注释开始,并且一直延续到"]]"为止。这种注释相当于C语言中的"/*…*/"。在注释当中,"[["和"]]"是可以嵌套的。
例如:
--[[ print(10) -- no action (comment) --]]
4. Lua语句
在Lua中,语句之间可以用分号";"隔开,也可以用空白隔开。一般来说,如果多个语句写在同一行的话,建议总是用分号隔开。
Lua 有好几种程序控制语句,如:
- 条件控制:if 条件 then … elseif 条件 then … else … end
- While循环:while 条件 do … end
- Repeat循环:repeat … until 条件
- For循环:for 变量 = 初值,终点值,步进 do … end
- For循环:for 变量1,变量2,… ,变量N in表或枚举函数 do … end
注意一下,for的循环变量总是只作用于for的局部变量,你也可以省略步进值,这时候,for循环会使用1作为步进值。
你可以用break来中止一个循环。
如果你有程序设计的基础,比如你学过Basic,C之类的,你会觉得Lua也不难。但Lua有几个地方是明显不同于这些程序设计语言的,所以请特别注意。
.语句块
语句块在C++中是用"{"和"}"括起来的,在Lua中,它是用do 和 end 括起来的。比如:
do print("Hello") end
你可以在 函数 中和 语句块 中定局部变量。
.赋值语句
赋值语句在Lua被强化了。它可以同时给多个变量赋值。
例如:
a,b,c,d=1,2,3,4
甚至是:
a,b=b,a -- 多么方便的交换变量功能啊。
在默认情况下,变量总是认为是全局的。假如你要定义局部变量,则在第一次赋值的时候,需要用local说明。比如:
local a,b,c = 1,2,3 -- a,b,c都是局部变量
.数值运算
和C语言一样,支持 +, -, *, /。但Lua还多了一个"^"。这表示指数乘方运算。比如2^3 结果为8, 2^4结果为16。
连接两个字符串,可以用".."运处符。如:
"This a " .. "string." -- 等于 "this a string"
.比较运算
< > <= >= == ~=
分别表示 小于,大于,不大于,不小于,相等,不相等
所有这些操作符总是返回true或false。
对于Table,Function和Userdata类型的数据,只有 == 和 ~=可以用。相等表示两个变量引用的是同一个数据。比如:
a={1,2}
b=a
print(a==b, a~=b) -- true, false
a={1,2}
b={1,2}
print(a==b, a~=b) -- false, true
.逻辑运算
and, or, not
其中,and 和 or 与C语言区别特别大。
在这里,请先记住,在Lua中,只有false和nil才计算为false,其它任何数据都计算为true,0也是true!
and 和 or的运算结果不是true和false,而是和它的两个操作数相关。
a and b:如果a为false,则返回a;否则返回b
a or b:如果 a 为true,则返回a;否则返回b
举几个例子:
print(4 and 5) --> 5
print(nil and 13) --> nil
print(false and 13) --> false
print(4 or 5) --> 4
print(false or 5) --> 5
在Lua中这是很有用的特性,也是比较令人混洧的特性。
我们可以模拟C语言中的语句:x = a? b : c,在Lua中,可以写成:x = a and b or c。
最有用的语句是: x = x or v,它相当于:if not x then x = v end 。
.运算符优先级,从高到低顺序如下:
^
not - (一元运算)
* /
+ -
..(字符串连接)
< > <= >= ~= ==
and
or
怎么确定一个变量是什么类型的呢?大家可以用type()函数来检查。Lua支持的类型有以下几种:
Nil 空值,所有没有使用过的变量,都是nil。nil既是值,又是类型。
Boolean 布尔值
Number 数值,在Lua里,数值相当于C语言的double
String 字符串,如果你愿意的话,字符串是可以包含'\0'字符的
Table 关系表类型,这个类型功能比较强大,我们在后面慢慢说。
Function 函数类型,不要怀疑,函数也是一种类型,也就是说,所有的函数,它本身就是一个变量。
Userdata 嗯,这个类型专门用来和Lua的宿主打交道的。宿主通常是用C和C++来编写的,在这种情况下,Userdata可以是宿主的任意数据类型,常用的有Struct和指针。
Thread 线程类型,在Lua中没有真正的线程。Lua中可以将一个函数分成几部份运行。如果感兴趣的话,可以去看看Lua的文档。
Maple中Chebyshev 级数展开的源代码:
> with(numapprox);
>interface(verboseproc = 2);
>print(chebyshev);
proc(f::{algebraic,procedure},eqn::{name,name=algebraic..algebraic,algebraic..algebraic},eps::numeric,size::integer)
option `Copyright (c) 1992 by the University of Waterloo. All rights reserved.`;
local f_is_operator,x,r,epsilon,oldDigits,a,b,evalhfOK,fproc,err,nofun,c,intf,tol,maxcoef,k,K,s,y,flags,dim,g;
if nargs<2 or 4<nargs then
error "wrong number (or type) of arguments"
elif type(f,'series') then
error "expecting an algebraic expression not of type series"
end if;
if type(eqn,`=`) then
f_is_operator:=false; x,r:=op(eqn)
elif type(eqn,'name') then
f_is_operator:=false; x:=eqn; r:=-1..1
else
f_is_operator:=true; r:=eqn
end if;
if type(f,'procedure') and type(eqn,{`=`,'name'}) then f_is_operator:=true end if;
if nargs<3 then epsilon:=Float(1,-Digits) elif Float(1,-Digits)<=eps then epsilon:=evalf(eps) else error "eps is less than 10^(-Digits)" end if;
oldDigits:=Digits;
Digits:=Digits+length(Digits+9)+1;
a:=evalf(op(1,r));
b:=evalf(op(2,r));
if not type([a,b],'[numeric,numeric]') then error "non-numeric end-points of interval" elif b<a then a,b:=b,a end if;
try
if f_is_operator then
fproc,evalhfOK:=`evalf/int/CreateProc`(f,_Operator,a,b,epsilon)
else fproc,evalhfOK:=`evalf/int/CreateProc`(f,x,a,b,epsilon)
end if
catch "function has a pole in the interval":
error "singularity in or near interval"
catch:
error
end try;
userinfo(1,'`numapprox:-chebyshev`',printf("procedure for evaluation is:\\n%a\\n",eval(fproc)));
if nargs=4 then dim:=size else dim:=487 end if;
flags:=array(1..3);
Assert(not assigned(intf));
if evalhfOK and Digits<=evalhf(Digits) then
try
c:=hfarray(1..dim); intf:=evalhf(`evalf/int/ccquad`(fproc,a,b,epsilon,dim,var(c),var(flags),true))
catch:
userinfo(1,'`numapprox:-chebyshev`',nprintf("evalhf mode unsuccessful - try using software floats"))
end try
end if;
if not assigned(intf) then try c:=array(1..dim); intf:=`evalf/int/ccquad`(fproc,a,b,epsilon,dim,c,flags,false) catch: error end try end if;
if type(intf,{'infinity','undefined'}) then error "singularity in or near interval" end if;
Digits:=oldDigits;
err:=flags[1];
nofun:=round(flags[2]);
if flags[3]<>0 or max(epsilon*abs(intf),0.001*epsilon)+Float(1,-Digits)<err then
if dim<=487 then
userinfo(2,'`numapprox:-chebyshev`',nprintf("try again using more function evaluations"));
s:=procname(fproc,convert(a,'rational')..convert(b,'rational'),epsilon,4375);
return if(type(eqn,'range'),eval(s),s(x))
else
error "singularity in or near interval"
end if
end if;
c:=map((v,d)->evalf(v/d),[seq(c[k],k=1..nofun)],nofun - 1);
maxcoef:=max(1,op(c));
tol:=1/5*epsilon*maxcoef;
for K from nofun by -1 to 1 while abs(c[K])<tol do end do;
for k by 2 to K while abs(c[k])<tol do end do;
if K<k and 0<K then
if f_is_operator then
error "even coefficients are zero - try f(x)/x"
elif a+1.0<>0. or b - 1.0<>0. then
userinfo(2,'`numapprox:-chebyshev`',nprintf("even coefficents are zero -- transform to interval -1..1"));
a:=op(1,r);
b:=op(2,r);
g:=eval(f,x=1/2*(b - a)*y+1/2*a+1/2*b);
s:=procname(g,y=-1..1,epsilon,dim);
s:=subs(y=2*x/(b - a) - (a+b)/(b - a),s)
else
userinfo(2,'`numapprox:-chebyshev`',nprintf("function is odd -- try f(x)/x"));
Digits:=Digits+2;
s:=procname(f/x,x=r,1/100*epsilon);
s:=numapprox:-chebmult('T'(1,x),s);
Digits:=Digits - 2;
if type(s,`+`) then s:=map(proc(t,tol) if abs(lcoeff(t))<tol then 0 else t end if end proc,s,tol) elif abs(lcoeff(s))<tol then s:=0 end if
end if;
s:=evalf(s);
s:=numapprox:-chebsort(s);
return s
end if;
c:=[seq(if(abs(c[k])<tol,0.,c[k]),k=1..max(K,1))];
a:=op(1,r);
b:=op(2,r);
y:=2*x/(b - a) - (a+b)/(b - a);
s:=1/2*c[1]*'T'(0,y)+add(c[k]*'T'(k - 1,y),k=2..K);
s:=numapprox:-chebsort(s);
if type(eqn,'range') then unapply(s,x) else s end if
end proc