public class TransferTest3{
public static void main(String args[]){
TransferTest3 test=new TransferTest3();
test.first();
}
public void first(){
int i=5;
Value v=new Value();
v.i=25;
second(v,i);
System.out.println(v.i);
}
public void second(Value v,int i){
i=0;
v.i=20;
Value val=new Value();
v=val;
System.out.println(v.i+" "+i);
}
}
class Value {
int i= 15;
}
定义一个int型的数组:int[] arr = new int[]{12,3,3,34,56,77,432};
让数组的每个位置上的值去除以首位置的元素,得到的结果,作为该位置上的新值。遍历新的数组。
public class Test3 {
public static void main(String[] args) {
/*
* 微软:
* 定义一个int型的数组:int[] arr = new int[]{12,3,3,34,56,77,432};
* 让数组的每个位置上的值去除以首位置的元素,得到的结果,作为该位置上的新值。遍历新的数组。
*/
int[] arr = new int[]{12,3,3,34,56,77,432};
//错误写法
for(int i= 0;i < arr.length;i++){
arr[i] = arr[i] / arr[0];
}
//正确写法1
for(int i = arr.length-1;i >= 0;i--){
arr[i] = arr[i] / arr[0];
}
//正确写法2
int temp = arr[0];
for(int i= 0;i < arr.length;i++){
arr[i] = arr[i] / temp;
}
}
}
/*
* int[] arr = new int[10];
* System.out.println(arr);//地址值?
*
* char[] arr1 = new char[10];
* System.out.println(arr1);//地址值?
*/
public class ArrayPrint {
public static void main(String[] args) {
int[] arr = new int[]{1,2,3};
//传进去的是一个Object的对象
System.out.println(arr);//地址值
char[] arr1 = new char[]{'a','b','c'};
//传进去的是一个数组,里面遍历数据了
System.out.println(arr1);//abc
}
}
练习4:将对象作为参数传递给方法
(1)定义一个Circle类,包含一个double型的radius属性代表圆的半径,一个findArea()方法返回圆的面积。
(2)定义一个类PassObject,在类中定义一个方法printAreas(),该方法的定义如下:
public void printAreas(Circle c,int time)
在printAreas方法中打印输出1到time之间的每个整数半径值,以及对应的面积。
例如,times为5,则输出半径1,2,3,4,5,以及对应的圆面积。
(3)在main方法中调用printAreas()方法,调用完毕后输出当前半径值。
public class Circle {
double radius; //半径
//返回圆的面积
public double findArea(){
return radius * radius * Math.PI;
}
}
public class PassObject {
public static void main(String[] args) {
PassObject test = new PassObject();
Circle c = new Circle();
test.printAreas(c, 5);
System.out.println("no radius is:" + c.radius);
}
public void printAreas(Circle c,int time){
System.out.println("Radius\t\tAreas");
//设置圆的半径
for(int i = 1;i <= time ;i++){
c.radius = i;
System.out.println(c.radius + "\t\t" + c.findArea());
}
//重新赋值
c.radius = time + 1;
}
}```