Java语言怎么才能使用线性搜索法在一个数组里判断一个整数是否出现在整个数组里,用一个函数输出对应的下标,这个代码是不是就是循环?循环和判断是怎么衔接的
你说的没错使用线性搜索法判断一个整数是否在数组中出现,通常需要使用循环和条件判断来进行衔接的
如图
代码如下
public class LinearSearch {
public static int linearSearch(int[] arr, int target) {
for (int i = 0; i < arr.length; i++) {
if (arr[i] == target) {
return i; // 返回对应的下标
}
}
return -1; // 如果未找到,则返回-1表示不存在
}
public static void main(String[] args) {
int[] arr = {5, 2, 9, 1, 7};
int target = 9;
int index = linearSearch(arr, target);
if (index != -1) {
System.out.println(target + " 在数组中的下标为 " + index);
} else {
System.out.println(target + " 不存在于数组中");
}
}
}
不知道你这个问题是否已经解决, 如果还没有解决的话:1. 图类型:
package com.usts.edu.graphic;
// 图的种类 有向图、有向网、无向图、无向网
public enum GraphKind {
UDG, // 无向图(UnDirected Graph)
DG, // 有向图(Directed Graph)
UDN, // 无向网(UnDirected Network)
DN; // 有向网(Directed Network)
}
2. 图的接口
package com.usts.edu.graphic;
//图的接口
public interface IGraph {
void createGraph();//创建一个图
int getVexNum(); // 返回顶点数
int getArcNum();// 返回边数
Object getVex(int v) throws Exception;// 返回v表示结点的值, 0 <= v < vexNum
int locateVex(Object vex);// 给定顶点的值vex,返回其在图中的位置,如果图中不包含此顶点,则返回-1
int firstAdjVex(int v) throws Exception; // 返回v的第一个邻接点,若v没有邻接点,则返回-1,其中0≤v<vexNum
int nextAdjVex(int v, int w) throws Exception;// 返回v相对于w的下一个邻接点,若w是v的最后一个邻接点,则返回-1,其中0≤v, w<vexNum
}
3. 创建图:
package com.usts.edu.graphic;
import java.util.Scanner;
public class MGraph implements IGraph {
public final static int INFINITY = Integer.MAX_VALUE;
private GraphKind kind;
private int vexNum, arcNum;
private Object[] vexs;
private int[][] arcs;
public MGraph() {
this(null, 0, 0, null, null);
}
public MGraph(GraphKind kind, int vexNum, int arcNum, Object[] vexs,
int[][] arcs) {
this.kind = kind;
this.vexNum = vexNum;
this.arcNum = arcNum;
this.vexs = vexs;
this.arcs = arcs;
}
public void createGraph() {
Scanner sc = new Scanner(System.in);
System.out.println("请输入图的类型");
GraphKind kind = GraphKind.valueOf(sc.next());
switch (kind) {
case UDG:
createUDG();
return;
case DG:
createDG();
return;
case UDN:
createUDN();
return;
case DN:
createDN();
return;
}
}
private void createUDG() {
};
private void createDG() {
};
private void createUDN() {
Scanner sc = new Scanner(System.in);
vexNum = sc.nextInt();
arcNum = sc.nextInt();
vexs = new Object[vexNum];
for (int v = 0; v < vexNum; v++)
vexs[v] = sc.next();
arcs = new int[vexNum][vexNum];
for (int v = 0; v < vexNum; v++)
for (int u = 0; u < vexNum; u++)
arcs[v][u] = INFINITY;
for (int k = 0; k < arcNum; k++) {
int v = locateVex(sc.next());
int u = locateVex(sc.next());
arcs[v][u] = arcs[u][v] = sc.nextInt();
}
}
private void createDN() {
Scanner sc = new Scanner(System.in);
vexNum = sc.nextInt();
arcNum = sc.nextInt();
vexs = new Object[vexNum];
for (int v = 0; v < vexNum; v++)
vexs[v] = sc.next();
arcs = new int[vexNum][vexNum];
for (int v = 0; v < vexNum; v++)
for (int u = 0; u < vexNum; u++)
arcs[v][u] = INFINITY;
for (int k = 0; k < arcNum; k++) {
int v = locateVex(sc.next());
int u = locateVex(sc.next());
arcs[v][u] = sc.nextInt();
}
}
public int getVexNum() {
return vexNum;
}
public int getArcNum() {
return arcNum;
}
public int locateVex(Object vex) {
for (int v = 0; v < vexNum; v++)
if (vexs[v].equals(vex))
return v;
return -1;
}
public Object getVex(int v) throws Exception {
if (v < 0 && v >= vexNum)
throw new Exception("第" + v + "个顶点不存在!");
return vexs[v];
}
public int firstAdjVex(int v) throws Exception {
if (v < 0 && v >= vexNum)
throw new Exception("第" + v + "个顶点不存在!");
for (int j = 0; j < vexNum; j++)
if (arcs[v][j] != 0 && arcs[v][j] < INFINITY)
return j;
return -1;
}
public int nextAdjVex(int v, int w) throws Exception {
if (v < 0 && v >= vexNum)
throw new Exception("第" + v + "个顶点不存在!");
for (int j = w + 1; j < vexNum; j++)
if (arcs[v][j] != 0 && arcs[v][j] < INFINITY)
return j;
return -1;
}
public GraphKind getKind() {
return kind;
}
public int[][] getArcs() {
return arcs;
}
public Object[] getVexs() {
return vexs;
}
public void setArcNum(int arcNum) {
this.arcNum = arcNum;
}
public void setArcs(int[][] arcs) {
this.arcs = arcs;
}
public void setKind(GraphKind kind) {
this.kind = kind;
}
public void setVexNum(int vexNum) {
this.vexNum = vexNum;
}
public void setVexs(Object[] vexs) {
this.vexs = vexs;
}
}
4. 实现搜索:
package com.usts.edu.graphic;
import com.usts.edu.Queue.LinkQueue;
/**
* Created by Guanzhong Hu
* Date :2020/3/30
* Description :
* Version :1.0
*/
public class GDSeach {
public final static int INFINITY = Integer.MAX_VALUE;
public static void CC_BFS(IGraph G) throws Exception {
boolean[] visited = new boolean[G.getVexNum()];
for (int v = 0; v < G.getVexNum(); v++)
visited[v] = false;
LinkQueue Q = new LinkQueue();
LinkQueue P = new LinkQueue();
int i = 0;
for (int v = 0; v < G.getVexNum(); v++) {
P.clear();
if (!visited[v]) {
visited[v] = true;
P.offer(G.getVex(v));
Q.offer(v);
while (!Q.isEmpty()) {
int u = (Integer) Q.poll();
for (int w = G.firstAdjVex(u); w >= 0; w = G.nextAdjVex(u,
w)) {
if (!visited[w]) {
visited[w] = true;
P.offer(G.getVex(w));
Q.offer(w);
}
}
}
System.out.println("图的第" + ++i + "个连通分量为:");
while (!P.isEmpty())
System.out.print(P.poll().toString() + " ");
System.out.println();
}
}
}
public static void main(String[] args) throws Exception {
Object vexs[] = { "A", "B", "C", "D", "E", "F", "G" };
int[][] arcs = { { 0, 1, INFINITY, 1, INFINITY, INFINITY, INFINITY },
{ 1, 0, 1, INFINITY, INFINITY, INFINITY, INFINITY },
{ INFINITY, 1, 0, 1, INFINITY, INFINITY, INFINITY },
{ 1, INFINITY, 1, 0, INFINITY, INFINITY, INFINITY },
{ INFINITY, INFINITY, INFINITY, INFINITY, 0, 1, INFINITY },
{ INFINITY, INFINITY, INFINITY, INFINITY, 1, 0, 1 },
{ INFINITY, INFINITY, INFINITY, INFINITY, INFINITY, 1, 0 }, };
MGraph G = new MGraph(GraphKind.UDG, 7, 6, vexs, arcs);
CC_BFS(G);
}
}
无向图的连通分量搜索是一种广度优先遍历的应用,当然还有如Dijkstra单源最短路径算法和Prim最小生成树算法都采用了和宽度优先搜索类似的思想。
源码地址:
https://gitee.com/jockhome/data_structure循环一下就可以了