Java第二次作业——数组和String类
(一)学习总结
1.学习使用Eclipse关联jdk源代码,查看String类的equals()方法,截图,并学习其实现方法。举例说明equals方法和==的区别。
(1)Eclipse关联jdk源代码如图

(2)equals方法和的区别
Spring 类中的“”使用来进行地址值比较的
Spring str1="hello",
String str2=new String ("hello");
String str3=str2;
System.out.printin("str1==str2-->"+(str1==str2));
System.out.printin("str1==str3-->"+(str1==str3));
System.out.printin("str2==str3-->"+(str2==str3));
equals()方法的作用是将内容进行比较
Spring str1="hello",
String str2=new String ("hello");
String str3=str2;
System.out.printin("str1 equals str2-->"+(str1.equals (str2)));
System.out.printin("str1 equals str3-->"+(str1.equals (str3)));
System.out.printin("str1 equals str3-->"+(str2.equals (str3)));
2.什么是构造方法?什么是构造方法的重载?下面的程序是否可以通过编译?为什么?
(1)在类中直接输入类型和参数,可多种类型和参数。程序可直接通过构造方法进行赋值使跟方便。且有default、privte、public三种访问权限。
构造方法格式
class类名称{
访问权限 类名称(类型1参数1,类型2参数2,......){
程序语句;
.......
}
只要每个构造方法的参数类型或参数个数不同,即可实现重载。
(2)
public class Test {
public static void main(String args[]) {
Foo obj = new Foo();
}
}
class Foo{
int value;
public Foo(int intValue){
value = intValue;
}
}
不可以进行编译
主函数调用构造方法时没有给定参数。
3.运行下列程序,结果是什么?查阅资料,分析为什么。
public class Test {
public static void main(String args[]) {
double a = 0.1;
double b = 0.1;
double c = 0.1;
if((a + b + c) == 0.3){
System.out.println("等于0.3");
}else {
System.out.println("不等于0.3");
}
}
}
结果为

必须用BigDecimal类精确计算
double类型没有办法是用二进制进行精确表示。
应将"double"改为"BigDecimal"如下
import java.math.BigDecimal;
public class test {
public static void main(String args[]) {
BigDecimal a = new BigDecimal ("0.1");
BigDecimal b = new BigDecimal ("0.1");
BigDecimal c = new BigDecimal ("0.1");
if(a.add(b).add(c).doubleValue()==0.3){
System.out.println("等于0.3");
}else {
System.out.println("不等于0.3");
}
}
}
4.运行下列程序,结果是什么?分析原因,应如何修改.
public class Test {
public static void main(String[] args) {
MyClass[] arr=new MyClass[3];
arr[1].value=100;
}
}
class MyClass{
public int value=1;
}
运行结果为

原因
只声明了对象数组
public class Test {
public static void main(String[] args) {
MyClass[] arr=new MyClass();
arr[1].value=100;
}
}
class MyClass{
public int value=1;
}
5.在一个10000次的循环中,需要进行字符串的连接操作,那么,应该使用String类还是StringBuffer类,为什么?性能有差异吗?能否写出测试代码证明你的结论。(可查阅资料)
应该使用StringBuffer类
因为Spring的实现需要通过不断修改对象的引用,所以性能比较差;使用StringBuffer可以使代码的性能有很大的提高。
所以说两者性能的有差异的。
String类
String str="liuxiaohua";
for(int i=0;i<100;i++){
str1+=i;
}
System.out.printin(Str1);
StringBuffer类
StringBuffer buf =new StringBuffer();
buf.append("Liuxiaohua");
for(int i=0;i<100;i++){
buf.append(i);
}
System.out.printin(buf);
6.自我总结
大一没有学会C语言,数组等方法不会使用 通过别人的帮助已经了解不是 并可以写出一些简单的程序来。
虽然只写出了两个程序,当以后会更加努力 争取刚上去。
(二)实验总结
1.评分系统
程序设计思路:利用二位数组的方法,利用循环先输出选手号与成绩让后输出求平均分时同时排序。
问题:不会使用动态数组,往数组里输入数据
在排序求平均分的基础上再求出最小值最大值等对我来说有点难度有点乱所以没能写出来。
解决方案:没有定义 定以后如图

2.Email验证
程序设计思路:在主函数中定于输出 ,在定义布尔类型进行调用。
问题:不知道如何定义使"@"在"."之前,以为直接写成"@."即可。
解决方案:
email.indexOf("@")<email.indexOf(".")

(三)代码托管
https://git.oschina.net/hebau_cs15/Java-CS02lcx.git

(四)学习进度条
| 代码行数 | 学习时间 | 本周学习内容 | |
|---|---|---|---|
| 第2-4周 | 150 /200 | 20 | 学习了... |
| 第5周 | 200/300 | 25 | 学习了数组的使用构造函数 String类等 |
| 第6周 |

浙公网安备 33010602011771号