数组拷贝操作 从数组默认下标 到拷到我指定的长度
来源:互联网 发布:c语言的编程环境 编辑:程序博客网 时间:2024/05/29 12:08
package com.heima.array;
public class AarryDemo3 {
public static void main(String[] args) {
/*
* 1.定义一个方法copyOf(int[] arr, int newLength),
* 功能:将数组arr中的newLength个元素拷贝到新数组中, 并将新数组返回, 从索引为0开始
*/
int[] arr = { 1, 2, 3, 4, 5, 6, 7, 8, 9 }; // 定义一个新的数组
getInfo(arr);// 遍历数组
arr = copyOf(arr, 5); // 给一个数组 给一个你要拷贝的长度 返回一个新的数组
System.out.println("拷贝后的数组是");
getInfo(arr);
}
//拷贝数组元素
public static int[] copyOf(int[] arr, int newLength) {
int[] newarr = new int[newLength]; // 定义一个数组给
for (int i = 0; i < newLength; i++) { //循环从数组下标0拷贝到newLength这个下标为值
newarr[i] = arr[i]; //把原始数组中取出来放入新数组中进去
}
return newarr; //返回一个数组
}
//遍历数组中的元素
public static void getInfo(int[] arr) {
for (int i : arr) {
if (i > 0) {
System.out.print(i + " ");
}
}
}
}
public class AarryDemo3 {
public static void main(String[] args) {
/*
* 1.定义一个方法copyOf(int[] arr, int newLength),
* 功能:将数组arr中的newLength个元素拷贝到新数组中, 并将新数组返回, 从索引为0开始
*/
int[] arr = { 1, 2, 3, 4, 5, 6, 7, 8, 9 }; // 定义一个新的数组
getInfo(arr);// 遍历数组
arr = copyOf(arr, 5); // 给一个数组 给一个你要拷贝的长度 返回一个新的数组
System.out.println("拷贝后的数组是");
getInfo(arr);
}
//拷贝数组元素
public static int[] copyOf(int[] arr, int newLength) {
int[] newarr = new int[newLength]; // 定义一个数组给
for (int i = 0; i < newLength; i++) { //循环从数组下标0拷贝到newLength这个下标为值
newarr[i] = arr[i]; //把原始数组中取出来放入新数组中进去
}
return newarr; //返回一个数组
}
//遍历数组中的元素
public static void getInfo(int[] arr) {
for (int i : arr) {
if (i > 0) {
System.out.print(i + " ");
}
}
}
}
1 0
- 数组拷贝操作 从数组默认下标 到拷到我指定的长度
- 删除指定下标的数组
- 指针碰到数组,不指定下标就默认保存数组的首地址,数组名本身就是首地址
- 指定数组的下标,如果数组元素等于指定值
- 数组的拷贝操作
- Java从一个数组指定位置拷贝到另外一个数组的指定位置-新法
- Java从一个数组指定位置拷贝到另外一个数组的指定位置-新法
- JQUERY从指定数组移除指定的值操作
- 数组的下标从0开始
- 两数之和二 给定一个整形的数组,找出其中的两个数使其和为某个指定的值,并返回这两个数的下标(数组下标是从0开始)。假设数组元素的值各不相同,则要求时间复杂度O(n),n为数组的长度
- 数组List默认长度
- 数组操作-将下标变成从0开始的连续数字
- 数组下标从0开始
- Java不指定长度的二维数组
- js 删除数组指定的值,不是下标(转载)
- gcc 下指定下标的字符串数组初始化
- 数组的下标应用
- 数组下标的本质
- DOM4J XML解析
- POJ 2411 Mondriaan's Dream 状态压缩
- 为bookStore添加权限【动态代理和注解】
- fl2440 内核移植
- tkinter的variable属性
- 数组拷贝操作 从数组默认下标 到拷到我指定的长度
- 数据库中常用的锁及其应用场景
- (转载/侵删)mvc与三层结构终极区别
- ElasticMaterial.cs
- Sort operation used more than the maximum 33554432 bytes of RAM
- 降维方法
- 在Docker环境下部署Kafka
- 240. Search a 2D Matrix II && 74. Search a 2D Matrix
- 简易计算器