JAVA,
分享于 点击 32750 次 点评:204
JAVA,
JAVA中的数组和字符串:
JAVA中的数组的定义和在C++中的一样,依旧采用 **数组类型 数组名【】**的形式进行定义,数组创建完之后系统会对各个基本类型的数组进行初始化赋值每种类型的赋值如下表所示:
数据类型 系统默认初值
boolean flase
byte 0
char ‘ \u0000’
short 0
int 0
long OL
float 0.0f
double 0.0
数组的引用依旧才用数组名+编号进行,对数组的索引编号从0开始。
实例:
public class TestJavafuzhi1{
public static void main(String[] args)
{
int i;
int a[];
a=new int[3];
for (i=0;i<3;i++){
System.out.println("a["+i+"]="+a[i]+",\t");
}
system.out.println("\n数组长度是:"+a.lenght);
}
}
数组的遍历:
遍历是采用对数组最为常见的操作,是按照某种规则,依次访问数组中的元素,如依次访问数组中的元素或者间隔特定的距离访问数组中的元素。
为方便数组的使用JAVA中提供了一些函数对数组进行操作:
public class Teaching {
public void doOneTeaching(){
Student []student = new Student[3];
student[0] = new Student();
student[1] = new Student();
Student []student = new Student[3];
student[0] = new Student();
student[1] = new Student();
student[2].setter("王五");
Teacher teacher = new Teacher();
teacher.setter("周炜");
TeachingMessage msg = new TeachingMessage();
msg.setter(teacher);
msg.setter(student);
student[0].doDuty(msg);
teacher.doDuty(msg);
student[0].doDuty(msg);
}
public static void main(String args[]){
Teaching teaching = new Teaching();
teaching.doOneTeaching();
}
public class Student extends UniversityPeople{
String name;
public void setter(String name){
this.name = name;
}
public String getter(){ return name;
}
void doDuty(TeachingMessage msg){
if(msg.state == TeachingState.BEIKE){
if(msg.state == TeachingStateTINGKE){
returnname;
voiddoDuty(TeachingMessagemsg)A
if(msg.state--leachingstate.BEIke
msg.statealeachingState.INGke,
System.out.print in(“老师“tmsg.etter10)getter()备完课后,去听课“);
if(msg.state == TeachingState.BUZHIZUOYE){
msg.state = T eachingState.ZUOZUOYE,
System.out. pr intln("老师"+msg.getter1().getter()"布置完课作业后,开始做作业“) ;
RepeatedStringEst imatorrnewRepeatedStr ingEst imator();
if(r.est imate("aad"))(
System.out.println("saaa是2——重复串)
字符串数组转字符串(只能通过for循环):
String[] str = {'a', 'b', 'd'};
StringBuffer sb = new StringBuffer();
for(int i = 0; i < str.length;i++){
sb.append(str[i]);
}
String s = sb.toString();
相关文章
- 暂无相关文章
用户点评