建立新数组来存放旧数组的逆序的数据为什么不可行?如图,代码如下:
修改如下:
public class Main {
public static void main(String[] args) {
int [] arrs = {40,12,50,45,123,458,456,785,85,96,74,22,68};
System.out.println("逆序前:");
for (int arr : arrs)
System.out.print(arr+" ");
int temp;
for (int i = 0; i < arrs.length / 2; i++)
{
temp = arrs[i];
arrs[i] = arrs[arrs.length - 1 - i];
arrs[arrs.length - 1 - i] = temp;
}
System.out.println("\n逆序后:");
for (int arr : arrs)
System.out.print(arr+" ");
}
}
Java数组必须指定长度,你开始创建的是长度为0的数组,当然不能放东西进去, int[] arr1 = new int[are.length]就可以了,如果不习惯用数组,建议用List,List可以自动拓展长度
不知道你这个问题是否已经解决, 如果还没有解决的话: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