A:数组遍历
int[] arr = {98,23,16,35,72};
写一个对象数组操作的功能,实现把数组中的数据按照如下格式返回:
"[98, 23, 16, 35, 72]"
B:查找数组中指定元素第一次出现的索引值。
int[] arr = {98,23,16,35,72};
查找23在数组中的索引值。
- public class Arr {
- public String arr2String(int[] arr) {
- String s = null;
- s = "[";
- for (int i = 0; i < arr.length; i++) {
- if (i == arr.length - 1) {
- s = s + arr[i];
- } else {
- s = s + arr[i] + ", ";
- }
- }
- s = s + "]";
- return s;
- }
- public int arrFindNum(int[] arr, int num) {
- for (int i = 0; i < arr.length; i++) {
- if (arr[i] == num) {
- return i;
- }
- }
- return -1;
- }
- }
复制代码
- public class ArrTest {
- private final static int[] ARR = { 98, 23, 16, 35, 72 };
- public static void main(String[] args) {
- Arr a = new Arr();
- System.out.println(a.arr2String(ARR));
- System.out.println(a.arrFind(ARR, 23));
- }
- }
复制代码 |
|