「java数组元素相加」怎么实现数组里的元素相加

博主:adminadmin 2023-01-21 04:00:11 55

本篇文章给大家谈谈java数组元素相加,以及怎么实现数组里的元素相加对应的知识点,希望对各位有所帮助,不要忘了收藏本站喔。

本文目录一览:

Java数组元素求和的问题

public class Test {

public static void main(String[] args) {

int[] numbers = { 68, 27, 95, 88, 171, 996, 51, 210};

int sum = 0;

for(int i : numbers) {

if(i%2 == 0) {

char[] strs = (i+"").toCharArray();

if(strs[strs.length-1] != 7 strs[strs.length-2] != 7) {

sum = sum+i;

}

}

}

System.out.println(sum);

}

}

java中如何求一个数组中元素的和.

import java.util.Scanner;

public class XiTi464 {

    public static void main(String[] args) {

        Scanner sr = new Scanner(System.in);

        System.out.print("输入数组元素个数:");

        int a = sr.nextInt();

        int score[] = new int[a];

        for (int i = 0; i  a; i++) {

            System.out.print("输入第" + (i + 1) + "个值:");

            score[i] = sr.nextInt();

        }

        int sum = arrSum(score);

        int max = arrMax(score);

        int min = arrMin(score);

        System.out.println("数组元素之和:" + sum);

        System.out.println("数组元素中最大值:" + max);

        System.out.println("数组元素中最小值:" + min);

        sr.close();

    }

    public static int arrSum(int arr[]) {

        int temp = 0;

        for (int i = 0; i  arr.length; i++) {

            temp += arr[i];

        }

        return temp;

    }

    public static int arrMax(int arr[]) {

        int temp = arr[0];

        for (int i = 1; i  arr.length; i++) {

            if (temp  arr[i])

                temp = arr[i];

        }

        return temp;

    }

    public static int arrMin(int arr[]) {

        int temp = arr[0];

        for (int i = 1; i  arr.length; i++) {

            if (temp  arr[i])    

                temp = arr[i];

        }

        return temp;

    }

}

JAVA数组元素如何相加

public class TypeOp {

public static void main(String[] args) {

int a[]= new int[5];//定义数组

a[0]=10;//赋值

a[1]=20;//赋值

int j = a[0]+a[1];

System.out.println(j);//求和

}

}

java如果把Array中的数组的值进行相加呢?

你的意思是说比如数组里面有5个对象,现在要改为6个或者更多? 未来你学集合就知道了,不过我这里自己封装一个类,里面就是操作Object数组的,增删插入指定位置都有,代码如下:

public class MyList {

private int size;

Object[] object = null;

Object[] temp;

int sequence = 0;

public MyList() {

this(1);

}

public MyList(int size) {

if (size = 0) {

throw new IllegalArgumentException("长度应大于0");

} else {

this.size = size;

this.object = new Object[this.size];

}

}

// Add, Insert, Delete, Find

public void add(Object obj) {

if (obj == null) {

throw new IllegalArgumentException("添加的对象不应为null");

} else {

if (sequence = size) {

this.size++;// 这里扩展空间方式随意,可以每次扩展两倍

temp = new Object[this.size];

System.arraycopy(object, 0, temp, 0, object.length);

object = temp;

temp = null;

}

object[sequence] = obj;

sequence++;

}

}

public void insert(int index, Object obj) {

if (index 0 || obj == null) {

throw new IllegalArgumentException("插入的索引值应不小于0,并且插入的对象不应为null");

} else {

if (index == object.length) {

add(obj);

} else if (index object.length) {

throw new IllegalArgumentException("数据越界,插入的索引不应不在数组范围内");

}

if (sequence = size) {

this.size++;

}

temp = new Object[this.size];

System.arraycopy(object, 0, temp, 0, index);

temp[index] = obj;

System.arraycopy(object, index, temp, index+1, object.length-index);

object = temp;

temp = null;

sequence++;

}

}

public void delete(int index) {

if (index 0 || indexthis.size) {

throw new IllegalArgumentException("索引应在数组范围内");

} else {

temp = new Object[--this.size];

System.arraycopy(object, 0, temp, 0, index);

System.arraycopy(object, index+1, temp, index, object.length-1-index);

object = temp;

temp = null;

sequence--;

}

}

public Object find(int index) {

if (index 0 || index this.size) {

throw new IllegalArgumentException("索引应大于0或不大于集合元素数");

} else {

return object[index];

}

}

public int size() {

return this.size;

}

}

关于java数组相加的一个问题

最后的System.out.print("数组反向相加后结果为:"+A[i]);

放到for循环外面去了 此时i的值为5了 所以报数组越界异常 把它放到for循环里面就行了

for(i=0;i5;i++){

A[i]=B[i]+C[i];

System.out.print("数组反向相加后结果为:"+A[i]);

}

JAVA数组元素求和

static int[] sum(int[] arr)

{

int[] s=new int[2];

for(int i=0;iarr.length;i+=2)

s[0]+=arr[i];

for(int i=1;iarr.length;i+=2)

s[1]+=arr[i];

return s;

}

java数组元素相加的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于怎么实现数组里的元素相加、java数组元素相加的信息别忘了在本站进行查找喔。

The End

发布于:2023-01-21,除非注明,否则均为首码项目网原创文章,转载请注明出处。