腾讯云授权服务中心
五万用户的选择,您身边的云计算顾问
发布日:2022-01-18 13:01 阅读数:
int[] nums = new int[100]; int[] nums = {1,2,3,4,5}; Object[] Objects = new Object[100];
在C++ 中表示为:
int nums[100];
package datastruction; import java.util.Arrays; public class MyArray { private int[] data; private int elementCount; private int length; public MyArray(int max) { length = max; data = new int[max]; elementCount = 0; } public void add(int value) { if (elementCount == length) { length = 2 * length; data = Arrays.copyOf(data, length); } data[elementCount] = value; elementCount++; } public int find(int searchKey) { int i; for (i = 0; i < elementCount; i++) { if (data[i] == searchKey) break; } if (i == elementCount) { return -1; } return i; } public boolean delete(int value) { int i = find(value); if (i == -1) { return false; } for (int j = i; j < elementCount - 1; j++) { data[j] = data[j + 1]; } elementCount--; return true; } public boolean update(int oldValue, int newValue) { int i = find(oldValue); if (i == -1) { return false; } data[i] = newValue; return true; } } // 测试类 public class Test { public static void main(String[] args) { MyArray myArray = new MyArray(2); myArray.add(1); myArray.add(2); myArray.add(3); myArray.delete(2); System.out.println(myArray); } }
编辑:航网科技 来源:腾讯云
本文版权归原作者所有 转载请注明出处
Copyright © 2011-2020 www.hangw.com. All Rights Reserved 深圳航网科技有限公司 版权所有 增值电信业务经营许可证:粤B2-20201122 - 粤ICP备14085080号
微信扫一扫咨询客服
全国免费服务热线
0755-36300002