一.首先,了解Java引用和传值的区别。
引用和传值的区别我这里是转载的一篇博文,感觉写的很棒很清楚,附原文链接:https://blog.youkuaiyun.com/weixin_36759405/article/details/82764339
传值:传递的是值的副本。方法中对副本的修改不会影响到调用方。
传引用:传递的是引用的副本,共用一个内存,会影响到调用方。因为传引用后指向的是同一个地址,修改的实际上也就是这个地址的值,所以调用方会受到影响。但是,如果对象被重新创建或赋值为null,即new会重新指向其他对象,不影响其原对象的值。
// 基本数据类型
public class ParamChangeValue {
public static void main(String[] args) {
int s = 1;
System.out.println("args = [" + s + "]");
change(s);
System.out.println("args = [" + s + "]");
}
private static void change(int i){
i = i* 5;
}
}
输出:
args = [1]
args = [1]
传值传递的是值的副本,不会影响到本身。
传引用:
// 对象
public class ObjectChangeValue {
public static class Score{
private int value;
public int getValue() {
return value;
}
public void setValue(int value) {
this.value = value;
}
}
public static void main(String[] args) {
Score score = new Score();
score.setValue(1);
System.out.println("args = [" + score.getValue() + "]");
change(score);
System.out.println("after args = [" + score.getValue() + "]");
}
private static void change(Score score){
score.setValue(2);
}
}
输出:
args = [1]
after args = [2]
传引用后指向的是同一个地址,修改的实际上也就是这个地址上的值,但如果对象被重新创建或赋值为null,即new会重新指向其他对象,不影响其原对象的值。
传String,Integer等final类型。
// String、Integer、Long等
public class StringChangeValue {
public static void main(String[] args) {
String s = "test1";
System.out.println("args = [" + s + "]");
change(s);
System.out.println("args = [" + s + "]");
}
private static void change(String i){
i = i + " test value";
}
}
输出:
args = [test1]
args = [test1]
基本类型(byte,short,int,char,double,float,long,Boolean)为传值,对象类型(Object,数组,容器)为传引用,String,Integer等final类型因为类的变量设为final属性,无法修改,只能重新赋值或生成对象,当Integer作为方法参数传递时,对其赋值会导致原有的引用被指向了方法内的栈地址,失去原有的地址指向,所以对赋值后的Integer做任何操作都不会影响原有值。
二. 数组的基础练习题,特别基础那种,基础好的自行跳过哦。
1.请将’A’,’B’,’C’存入数组,然后再输出
public class Array {
public static void main(String[] args) {
//请将’A’,’B’,’C’存入数组,然后再输出
char[] a=new char[]{'A','B','C'};
for(char i:a){
System.out.println(i);
}
}
}
2.请将”我” “爱” “你”存入数组,然后正着和反着输出
public class Array {
public static void main(String[] args) {
//请将”我” “爱” “你”存入数组,然后正着和反着输出
String[] a={"我","爱","你"};
//正序
for(String i:a){
System.out.println(i);
}
//倒序
for(int i=2;i>=0;i--){
System.out.println(a[i]);
}
}
}
3.输入10个整数存入数组,然后复制到b数组中输出
public class Array {
public static void main(String[] args) {
//输入10个整数存入数组,然后复制到b数组中输出
int[] a=new int[10];
int[] b=new int[a.length];
Scanner in=new Scanner(System.in);
for(int i=0;i<10;i++){
a[i]=in.nextInt();
b[i]=a[i];
}
// for(int j:a){
// b[j]=a[j];
// System.out.println(b[j]);
// }
for(int k:b){
System.out.println(k);
}
}
}
4.声明一个int型的数组,循环接收8个学生的成绩,计算这8个学生的总分及平均分、最高分和最低分。
public class Array {
public static void main(String[] args) {
//声明一个int型的数组,循环接收8个学生的成绩,计算这8个学生的总分及平均分、最高分和最低分。
double a[]=new double[8];
Scanner input=new Scanner(System.in);
double sum=0;
double avg=0;
for(int i=0;i<a.length;i++){
a[i]=input.nextInt();
sum+=a[i];
}
double max=a[0];
double min=a[0];
for(int j=0;j<a.length;j++){
while(a[j]>max){
max=a[j];
}
while (a[j]<min){
min=a[j];
}
}
avg=sum/a.length;
System.out.println("总分:"+sum+"平均分:"+avg+"最高分:"+max+"最低分:"+min);
}
}
5.输出10000以下的所有质数程序分析。判断素数的方法:用一个数分别去除2到sqrt(这个数),如果能被整除, 则表明此数不是素数,反之是素数。
这题吧。。。我没用数组,我就当输入一个数,然后判断素数写的。
public class Array {
public static void main(String[] args) {
int x;
Scanner input=new Scanner(System.in);
x=input.nextInt();
boolean fl=true;
for(int j=2;j<=Math.sqrt(x);j++){
if(x%j==0){
fl=false;
}else {
fl=true;
}
}
System.out.println(fl);
}
}
6.定义一个长度为10的整型数组,循环输入10个整数。 /然后将输入一个整数,查找此整数,找到 输出下标,没找到给出提示。
public class Array {
public static void main(String[] args) {
//定义一个长度为10的整型数组,循环输入10个整数。
// 然后将输入一个整数,查找此整数,找到 输出下标,没找到给出提示。
int a[]=new int[10];
int tem ;
Scanner input=new Scanner(System.in);
tem=input.nextInt();
boolean fl=true; //有
int i=0;
for(i=0;i<a.length;i++){
a[i]=input.nextInt();
if(a[i]==tem){
fl=true;
break;
}else{
fl=false;
}
}
if(fl){
System.out.println(i);
}else{
System.out.println("数组中并无此数");
}
}
}
我这里用来一个break, break对判断语句只是判断,并无作用,对循环语句会跳出离break最近的一个循环,所以此处的作用是找到那个数了就跳出for循环,如果不跳出的话fl会变,结果就错了。
题外话:
我就找几个最普通的练习,哪里如果有错误我会改正的,然后如果想深入练习或者多练习还是得刷题,综合运用。然后我的代码的话,建议只是自己写完了看几眼,当然自己写完了不看也行,毕竟我的思路也许有些人不喜欢或者有些小伙伴可能有比这个好很多的思路嘛,还是想告诉大家,自己思考最重要!加油!