int[] score = new int [1];数组声明是在栈内存里面开辟的,而为数组开辟的空间在堆内存;
方法的重载,意思是方法名相同,但是参数的类型和个数不同;
java.util.Arrays.sort(数组名称),排序方法;
数组的引用传递就是堆内存的使用权,可以将一个数组传递到方法之中,传递的时候不需要写上“[]”,直接写上名字。方法中对数组所做的修改都会被保留下来。
System.arraycopy()方法,接收参数。与copy方法相同
习题1、求1!+2!+....+30!的和,使用方法完成。
public class factorial {//高精度没学好,搞了超久。
public static void main(String[] args) {
// TODO code application logic here
myFactorial("20");
}
public static void myFactorial(String n) {
BigInteger i = new BigInteger("1");
BigInteger x = new BigInteger("1");
BigInteger sum = new BigInteger("0");
BigInteger y = new BigInteger("1");
BigInteger h = new BigInteger(n);
while (i.compareTo(h) <= 0) {
sum = sum.add(y);
i = i.add(x);
y = y.multiply(i);
//System.out.println("a");
}
System.out.println("阶乘总数是: " + sum);
}
}
习题2、定义一个有整数组成的数组,求出奇数、偶数的个数;
public class oddEven {
public static void main(String[] args) {
int num[]={1,2,3,4,5,6,7,8,9,10,11};
solve(num);
// TODO code application logic here
}
public static void solve(int temp[]){
int odd=0;
int even=0;
for(int i=0;i
if(temp[i]%2==0){
even++;
}else{
odd++;
}
}
System.out.println("奇数有: "+odd+" 偶数有:"+even);
}
}
习题3、把数组中包含0的删除;
public class delete {//用了超多种方法,终于搞出来了,擦擦
public static void main(String[] args) {
int num[] = {1, 3, 0, 8,12,0,0,0,1,2};
cancel(num);
// TODO code application logic here
}
public static void cancel(int temp[]) {
int x = 0;
int len = temp.length;
int newLen = 0;
for (int i = 0; i < len; i++) {
if (temp[i] == 0) {
x++;
}
}
newLen = len - x;
int arr[] = new int[newLen];
for (int i = 0, j = 0; i < len; i++) {
if (temp[i] != 0) {
arr[j++] = temp[i];
}
}
for (int i = 0; i < newLen; i++) {
System.out.print(arr[i]);
}
}
}
习题4、定义一个整型数组,求出数组元素和、数组元素的最大值和最小值,并输出结果;
public class myArray {
public static void main(String[] args) {
// TODO code application logic here
int num[] = {1, 2, 3, 4, 5, 6, 7, 8, 9};
count(num);
}
public static void count(int temp[]) {
int sum = 0;//数组总和
int max = temp[0];//数组最大值
int min = temp[0];//数组最小值
for (int i = 0; i < temp.length; i++) {
if (temp[i] > max) {
max = temp[i];
}
if (temp[i] < min) {
min = temp[i];
}
sum = sum + temp[i];
}
System.out.println("");
System.out.print("最大值:" + max + " 最小值:" + min + " 数组和: " + sum);
}
}
习题5、给出10个整型(int 型),然后任意查询一个数字是否包含在该数族内;
public class exist {//so easy
public static void main(String[] args) {
// TODO code application logic here
int num[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 0};
int a = 13;
count(num, a);
}
public static void count(int temp[], int num) {
boolean exist = false;
for (int i = 0; i < temp.length; i++) {
if (temp[i] == num) {
exist = true;
}
}
if (exist == false) {
System.out.println("不存在");
} else {
System.out.println("存在");
}
}
}
习题6、定义一个包含10个元素的数组,对其进行赋值,使每个元素的值等于其下标,然后输出;最后将这个数组倒置输出;
public class index {
public static void main(String[] args) {
// TODO code application logic here
int n = 10;
int[] num = new int[n];
array(num, n);
}
public static void array(int temp[], int n) {
for (int i = 0; i < n; i++) {
temp[i] = i;
}
System.out.print("数组:");
for (int i = 0; i < n; i++) {
System.out.print("\t" + temp[i]);
}
System.out.println("");
for (int i = n - 1; i >= 0; i--) {
System.out.print("\t" + temp[i]);
}
}
}
习题7、给十个老师打分,对十个老师的打分找到最高分;
public class teacher {
public static void main(String[] args) {
// TODO code application logic here
String name[] = {"A", "B", "C", "D", "E", "F", "G", "H", "I", "J"};
int mark[] = {1, 3, 5, 2, 1, 7, 4, 9, 4, 2};
grade(name, mark);
}
public static void grade(String temp[], int mark[]) {
int max = mark[0];//定义最大的分数
int x = 0;//保存第几个分数最大
for (int i = 0; i < mark.length; i++) {
if (mark[i] > max) {
max = mark[i];
x = i;
}
}
System.out.println("最高分的老师是: " + temp[x] + " 他的分数是:" + mark[x]);
}
}
习题8、有30个0~9之间的数字,分别统计0~9这10个数字分别出现多少次;
public class countNumber {
public static void main(String[] args) {
// TODO code application logic here
int n[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 0, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30};
count(n);
}
public static void count(int temp[]) {
int[] num = new int[10];
for (int i = 0; i < temp.length; i++) {
for (int j = 0; j <= 9; j++) {
if (temp[i] == j) {
num[j]++;
}
}
}
for (int i = 0; i <= 9; i++) {
System.out.print(i + "出现了 " + num[i] + " 次" + "\t");
}
}
}
习题9、定义一个整型数组,保存10个数据,利用程序完成将最大值保存在数组中第一个元素的操作;
public class save {
public static void main(String[] args) {
// TODO code application logic here
int num[] = {1, 2, 3, 4, 5, 6, 7, 8, 9};
keep(num);
}
public static void keep(int temp[]) {
int max = temp[0];//保存最大值
int x = 0;//保存下标
for (int i = 0; i < temp.length; i++) {
if (temp[i] > max) {
max = temp[i];
x = i;
}
}
int c;//用来交换的
c = temp[0];
temp[0] = max;
temp[x] = c;
for (int i = 0; i < temp.length; i++) {
System.out.print(temp[i]);
}
}
}
习题10、在一个排序好的数组中添加一个数字,将添加后的数字插入到数组适合的位置;
public class insert {
public static void main(String[] args) {
// TODO code application logic here
int oldNum[] = {1, 2, 3, 4, 6, 7, 8, 9};
int num = 5;
charu(oldNum, num);
}
public static void charu(int temp[], int num) {
int[] arr = new int[temp.length + 1];
boolean x = false;//记录是否已插入
for (int i = 0; i < temp.length; i++) {
if (x == false) {
if (temp[i] <= num) {
arr[i] = temp[i];
} else {
arr[i] = num;
x = true;
}
}
if (x == true) {
arr[i + 1] = temp[i];
}
}
for (int i = 0; i < temp.length + 1; i++) {
System.out.print(arr[i]);
}
}
}
第二天学习java,好像还挺简单,努力加油!!