Java数组元素查找 接收并打印一个未知长度的数组

package cn.nrsc.demo;

import java.util.Scanner;
//****************************************************************************//
//作者认为该程序最主要的功能是可以实现<<接收并打印一个未知长度的数组>>的效果  //
//*************************************************************************** //
/*题目出处:
需求:数组元素查找(查找指定元素第一次在数组中出现的索引)++++++++++作者扩展:查找指定元素在数组中出现的所有位置
	(1)给定数组int[] arr = {5,7,3,2,5};+++++++++++       可以实现接收并打印一个未知长度的数组的效果.
	(2)要查询的元素通过键盘录入的方式确定
	(3)定义一个查找数组元素第一次出现位置的方法(注,要查找的元素就是键盘录入的数据), 
	如果没有找到返回-1
分析:
1. 定义数组
2. 从键盘输入一个数: Scanner
3. 调用方法,查询键盘录入的数在数组的哪个位置
4. 输出方法返回的结果
 */
public class Demo07ArrayFind {

	public static void main(String[] args) {
		// 1. 定义数组
		int[] arr = { 5, 7, 3, 2, 5, 9, 0, 9 };

		// 2. 从键盘输入一个数: Scanner
		@SuppressWarnings("resource")
		Scanner sc = new Scanner(System.in);
		System.out.println("请输入你要查找的数");
		int srcNumber = sc.nextInt();

		// 3. 调用方法,查询键盘录入的数在数组中第一次出现在哪个位置
		System.out.println("调用find_First_IndexInArray返回结果");
		System.out.println("=============================");
		int index_Frist = find_First_IndexInArray(arr, srcNumber);

		// 4. 输出方法返回的结果
		if (index_Frist == -1) {
			System.out.println("你要查找的数不存在");
		} else {
			System.out.println("你要查的数第一次在数组中出现时的角标(索引)为: " + index_Frist);
		}

		System.out.println();
		// 5.调用方法,查询键盘录入的数在数组中的哪个位置(所有的位置)
		System.out.println("调用find_All_IndexInArray返回结果");
		System.out.println("=============================");
		char[] index_All = find_All_IndexInArray(arr, srcNumber);
		if (index_All.length != 0) {
			// 将查询得到的数组打印成[1,2,3,4,5,6]的形式
			System.out.print("你要查的数在数组中的位置为(所有位置)为: [");
			for (int i = 0; i < index_All.length; i++) {
				if (i == index_All.length - 1) {
					System.out.println(index_All[i] + "]");
				} else {
					System.out.print(index_All[i] + ",");
				}
			}
		}
	}

	/*
	 * 方法名: findIndexInArray 参数: int[]array, int number 返回值: int
	 */
	// ********************该方法值得细细品味*****************************
	// ******************************************************************
	public static int find_First_IndexInArray(int[] array, int number) {
		// 定义变量记录元素在数组的位置
		int index = -1;
		// 遍历数组,查看number在数组当中是否存在
		for (int i = 0; i < array.length; i++) {
			if (array[i] == number) {
				index = i;
				break;
			}
		}
		// 返回索引
		return index;
	}

	// ********************//这个思路会经常用到***********************************
	// ****************实现接收并打印一个未知长度的数组的效果*****************************
	public static char[] find_All_IndexInArray(int[] array, int number) {
		// 定义变量记录元素在数组的位置
		int index = -1;

		// 定义一个字符窜记录所有满足条件的索引值
	
		String str = new String();
		str = "";

		// 遍历数组,查看number在数组当中是否存在,如果存在将其索引值存入str中
		for (int i = 0; i < array.length; i++) {
			if (array[i] == number) {
				index = i;
				str = str + i;
			}
		}
		if (index == -1) {
			System.out.println("你要查找的数不存在");
		}

		// 将所有满足条件的索引值转换成字符
		char[] charray = str.toCharArray();
		// int[] result = new int[charray.length];
		// for (int i = 0; i < charray.length; i++) {
		// result[i] = (int) charray[i];
		// }
		return charray;// 返回索引
	}

}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值