当前位置:首页 » 操作系统 » 数据结构java源码

数据结构java源码

发布时间: 2023-05-19 12:55:57

java数据结构与算法

/*给出一组数:1,4,7,10,。。。,3N+1,n为正整数 找出下一个数和第十个数
* 写出伪代码和Java代码 谢谢啦*/
public static String getNumber(int number){
//定义一个初始值0
判断如果number为0或是负数
return "您输入的不对请输入正整数(除0意外的自然数)";
否则就进行运算
根据算式 3*(number-1)+1进行运算
返回运算结果
}
}

public static void main(String [] args){
打印输出
}

/*给出一组数:1,4,7,10,。。。,3N+1,n为正整数 找出下一个数和第十个数
* 写出伪代码和Java代码 谢谢啦*/
public static String getNumber(int number){
int result = 0;
if(number == 0 || number < 0){
return "您输入的不对请输入正整数(除0意外的自然数)";
}else{
result = 3*(number-1)+1;
return ""+result;
}
}

public static void main(String [] args){
System.out.println("第五项:"+Test1.getNumber(5));
System.out.println("第十项:"+Test1.getNumber(10));
}

希望对您有帮助

❷ 求程序代码(java版的数据结构)

3个燃罩class,运行UI.java。
******
public class CircuitException extends Exception {public CircuitException(){}}
*****
import java.util.LinkedList;
public class GPS {
public static final int MAX = 65535;
public GPS(int maxSize){
graph = new Graph(maxSize);
}
public GPS(){
graph = new Graph();
}
public Graph graph;
public static void main(String args[]){
GPS gps = new GPS();
try {
gps.graph.addEdge("a", "b", 1);
gps.graph.addEdge("a", "c", 1);
gps.graph.addEdge("b","d" , 1);
gps.graph.addEdge("c","d"皮迹闹 , 1);
gps.graph.addEdge("d","e" , 1);
gps.graph.addEdge("d","f" , 1);
gps.graph.addEdge("e","t" , 2);
gps.graph.addEdge("f","t" , 1);
LinkedList list = gps.graph.getPath("a", "d");
for(int i = 0 ; i < list.size() ; i++){
System.out.print(list.get(i));

}System.out.println();
} catch (CircuitException e) {
System.out.println("出现了自环!"州首);
}
gps.graph.showGraph();
System.out.println(gps.graph.gap);
}
public class Graph{
public int Zuida = 50;
public int chang = 0;
public Jiao[] vertex;
public double gap;
public Graph(){
vertex = new Jiao[Zuida];
}
public Graph(int maxSize){
this.Zuida = maxSize;
vertex = new Jiao[maxSize];
}
public void addVertex(String name){
vertex[chang++] = new Jiao(name);
}
public void addEdge(String v1, String v2,double edge) throws CircuitException{
//先找到v1;
if(v1.equals(v2))
throw new CircuitException();
Jiao from = null;
Jiao to = null;
for(int i = 0 ; i < chang ; i++){
if(vertex[i].name.equals(v1)){
from = vertex[i];
}else if(vertex[i].name.equals(v2)){
to = vertex[i];
}
}
if(from == null){
this.addVertex(v1);
from = this.vertex[chang-1];
}
if(to == null){
this.addVertex(v2);
to = this.vertex[chang-1];
}//已经找到v1和v2;
//没有检测是否v1 v2边已经存在!
//加入边。
Jiao v1adj = new Jiao(v2);
v1adj.edge = edge;
Jiao v2adj = new Jiao(v1);
v2adj.edge = edge;
//添加联系
//检查联系是否已经存在
Jiao temp = from;
while(temp.next!=null){
Jiao temppar = temp;
temp = temp.next;
if(temp.name.equals(v1adj.name)){
temppar.next = temp.next;
}
}
v1adj.next = from.next;
from.next = v1adj;
//v2adj.next = to.next;
//to.next = v2adj;
}
//假设要找的必然存在,不用想是否不在
public LinkedList getPath(String v1 ,String v2){
int count = 0;
//System.out.println(count++);
boolean found[] = new boolean[chang];
double distance[] = new double[chang];
int to = 0;
Jiao from = null;
for(int i = 0 ; i < chang ; i++){
found[i] = false;
distance[i] = MAX;
}
for(int i = 0 ; i < chang ; i++){
if(vertex[i].name.equals(v1)){//找到始发地
from = vertex[i];
distance[i] = 0;
found[i] = true;
//System.out.println(count++);
}
if(vertex[i].name.equals(v2)){//找到目的地
to = i;
//System.out.println(count++);
}
}
//必须先准备好路径!
Jiao forCount = from;
int degree = 0;
while(forCount!=null){
degree++;
forCount=forCount.next;
}
LinkedList[] list = new LinkedList[degree];
int [] mark = new int[degree];
for(int i = 0 ; i < degree ; i++){
list[i]=new LinkedList();
mark[i]=MAX;
}
int test=0;
int count2 = 0;
int count3 = 0;
//System.out.println(count+++"xx");
while(!found[to]&&test++<100){
//System.out.println(count+++"FIRST");

//开始时from到所有都是最大值。
//找到标记了的节点
//找标记了的节点邻接的未标记的节点。
//得到最短的边,并标记。
//更新现有路径
double min = MAX;
int address = -1;
int father = -1;
for(int i = 0 ; i < chang ; i++){//对于已经找到的顶点寻找最小的往后的距离。
if(found[i]){//找到了的。
Jiao temp = vertex[i];
while(temp!=null){//vertex的邻接顶点~~

//先看temp的号码~
int tempNumber = -1;
for(int j = 0 ; j < chang ; j++){
if(vertex[j].name.equals(temp.name)){
tempNumber = j;
break;
}
}
if(!found[tempNumber]){//如果是还没有找到的~
double dist = distance[i]+temp.edge;
if(dist < min){
min = dist;
father = i;
//System.out.println(" "+min);
address = tempNumber;
}
}
temp = temp.next;
}

}

}found[address] = true;
distance[address] = min;
//添加到已有路径中去!
//知道father
for(int i = 0 ; i < degree ; i++){
if(list[i].isEmpty()||list[i].getLast().equals(vertex[father].name)){
list[i].addLast(vertex[address].name);
break;
}
}
}
for(int i = 0 ; i < degree ; i++){
if(list[i].isEmpty())
continue;
else{
if(list[i].getLast().equals(v2)){
gap=0;
//先求出gap
Jiao pre = from;
Jiao nex = null;
for(int j = 0 ; j < list[i].size() ; j++){
for(int k = 0 ; k < chang ; k++){
if(vertex[k].name.equals(list[i].get(j))){
nex = vertex[k];break;
}
}

while(pre.next!=null){//找到下一个的长度
pre = pre.next;
//System.out.println(nex.name +"nex.name");
if(pre.name.equals(nex.name)){
gap+=pre.edge;
//System.out.println(" gap2 "+gap);
}
}
pre = nex;
}
//System.out.println(gap+"gap");
return list[i];
}
}
}
return null;
}
public void showGraph(){
Jiao temp;
for(int i = 0 ; i < chang ; i++){
temp = vertex[i];
while(temp!=null){
System.out.print(temp.name+temp.edge+" ");
temp = temp.next;
}System.out.println();
}System.out.println("Show Over!");
}
}

public class Jiao{
public String name;
public Jiao next = null;
public double edge;
public Jiao(String name){
this.name = name;
}
}
}
******
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.LinkedList;
import javax.swing.JButton;
import javax.swing.DefaultListModel;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JSeparator;
import javax.swing.JTextField;
import javax.swing.SwingConstants;

public class UI extends JFrame implements ActionListener{

private JTextField textField_5;
private JTextField textField_4;
private JList list_1;
private JList list;
private JTextField textField_1;
private JTextField textField_3;
private JTextField textField_2;
private JTextField textField;
private DefaultListModel model = new DefaultListModel();
private DefaultListModel model_1 = new DefaultListModel();
/**
* Launch the application
* @param args
*/
public static void main(String args[]) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
UI frame = new UI();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}

/**
* Create the frame
*/
public UI() {
super();
setTitle("GPS寻路");
getContentPane().setLayout(null);
setBounds(100, 100, 500, 375);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

final JScrollPane scrollPane = new JScrollPane();
scrollPane.setBounds(11, 36, 221, 193);
getContentPane().add(scrollPane);

list = new JList(model);
scrollPane.setViewportView(list);

final JScrollPane scrollPane_1 = new JScrollPane();
scrollPane_1.setBounds(253, 36, 218, 193);
getContentPane().add(scrollPane_1);

list_1 = new JList(model_1);
scrollPane_1.setViewportView(list_1);

final JLabel label = new JLabel();
label.setText("从");
label.setBounds(10, 249, 24, 18);
getContentPane().add(label);

final JLabel label_1 = new JLabel();
label_1.setText("到");
label_1.setBounds(11, 273, 24, 18);
getContentPane().add(label_1);

textField = new JTextField();
textField.setBounds(50, 247, 103, 22);
getContentPane().add(textField);

textField_2 = new JTextField();
textField_2.setBounds(50, 271, 103, 22);
getContentPane().add(textField_2);

final JLabel label_2 = new JLabel();
label_2.setText("距离");
label_2.setBounds(11, 297, 37, 18);
getContentPane().add(label_2);

textField_3 = new JTextField();
textField_3.setBounds(50, 295, 103, 22);
getContentPane().add(textField_3);

final JButton button = new JButton();
button.setText("添加");
button.setBounds(155, 250, 73, 28);
getContentPane().add(button);

final JButton button_1 = new JButton();
button_1.setText("删除");
button_1.setBounds(155, 285, 73, 28);
getContentPane().add(button_1);

final JLabel label_3 = new JLabel();
label_3.setText("距离:");
label_3.setBounds(253, 297, 39, 18);
getContentPane().add(label_3);

textField_1 = new JTextField();
textField_1.setBounds(293, 295, 86, 22);
getContentPane().add(textField_1);

final JButton button_2 = new JButton();
button_2.setText("显示路径");
button_2.setBounds(385, 249, 86, 68);
getContentPane().add(button_2);

final JLabel label_4 = new JLabel();
label_4.setText("路径表示");
label_4.setBounds(11, 10, 66, 18);
getContentPane().add(label_4);

final JLabel label_5 = new JLabel();
label_5.setText("最佳路径");
label_5.setBounds(253, 12, 66, 18);
getContentPane().add(label_5);
//
button.addActionListener(this);
button_1.addActionListener(this);
button_2.addActionListener(this);

final JLabel label_6 = new JLabel();
label_6.setText("从");
label_6.setBounds(253, 249, 24, 18);
getContentPane().add(label_6);

textField_4 = new JTextField();
textField_4.setBounds(293, 247, 86, 22);
getContentPane().add(textField_4);

final JLabel label_7 = new JLabel();
label_7.setText("到");
label_7.setBounds(253, 273, 24, 18);
getContentPane().add(label_7);

textField_5 = new JTextField();
textField_5.setBounds(293, 271, 86, 22);
getContentPane().add(textField_5);

final JSeparator separator = new JSeparator();
separator.setOrientation(SwingConstants.VERTICAL);
separator.setBounds(239, 10, 8, 317);
getContentPane().add(separator);
}

@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if(e.getActionCommand().equals("添加")){

try{String from = textField.getText();
String to = textField_2.getText();
if(from.equals(to)){
JOptionPane.showMessageDialog(null, "始点与终点不能相同");
return;
}
if(from.equals("")||to.equals("")){
JOptionPane.showMessageDialog(null, "添加不能为空");
return;
}for(int i = 0 ; i < model.size() ; i ++){
if(model.get(i).toString().substring(0, model.get(i).toString().indexOf(":")).equals(
from+"->"+to))
model.remove(i);
}
double length = Double.parseDouble(textField_3.getText());
model.addElement(from+"->"+to+": "+length);

}catch(Exception e1){
JOptionPane.showMessageDialog(null, "距离为数字值");
}
}
if(e.getActionCommand().equals("删除")){
for(int i = 0 ; i < model.size() ; i++){
if(list.isSelectedIndex(i))
model.remove(i);
}
}
if(e.getActionCommand().equals("显示路径")){
try{
model_1.removeAllElements();
GPS gps = new GPS();
String full,from,to;
double length;
for(int i = 0 ; i < model.size() ; i++){
full = model.get(i).toString();
from = full.substring(0,full.indexOf("-"));
to = full.substring(full.indexOf("-")+2,full.lastIndexOf(":"));
length = Double.parseDouble(full.substring(full.indexOf(":")+1, full.length()-1));
//System.out.println(from);
//System.out.println(to);
try {
gps.graph.addEdge(from, to, length);
System.out.println(from +" "+ to);
} catch (CircuitException e1) {
System.out.println("有环存在");
}
}LinkedList list = gps.graph.getPath(textField_4.getText(), textField_5.getText());
model_1.addElement(textField_4.getText());
for(int i = 0 ; i < list.size() ; i++){
model_1.addElement(list.get(i));
}//计算路径长度
textField_1.setText(""+gps.graph.gap);
}catch(Exception e1){
JOptionPane.showMessageDialog(null, "没有找到有关节点");
}
}
}
}

❸ java游戏源码怎么看模型数据

从Java游戏源码中获取模型数据,通常需要查看游戏中的模型加载代码。代码中通常会使用一些开源库或者自定义的工具类来读取模型文件,并将读取锋段到的数据转换为程序中使用的数据结构。以下是一些常用的Java模型加载库和工具类:

1. jMonkeyEngine:jMonkeyEngine是一个开源的游戏引擎,可以用来创建Java游戏,并提供了一些用于读取和加载3D模型的工具类。

2. LWJGL:手衫LWJGL是一个轻量级的Java游戏库,它提供了访问OpenGL、OpenAL和输入设备等底层功能的接口。可以使用它来读取和加载3D模型,并将模型数据转换为OpenGL所需的数据格式。

3. Assimp:Assimp是一个开源的模型导入库,支持多种模型格式,包括3D Studio Max、Collada、OBJ等。可以使用它来读取和加载模型,并将模型数据转换为程序中使用的数据结构。

4. 自定义工具类:如果游戏源码没有使用上述库或者工具类,也可以自己编写工具类来读取和加载模型数据。通常需要根据模型文件格式,逐个读取文件中的数据,并将其转换为程序中使用的数据结构。

无论是使用开源库还是自定义工具银薯誉类,都需要了解模型文件的格式和结构,以正确地读取和解析模型数据。一般来说,模型文件通常包含顶点坐标、法线、纹理坐标等信息,需要将这些信息转换为程序中使用的数据结构,然后传递给OpenGL或者其他引擎进行渲染。

❹ 用java实现一个数据结构!

import java.io.IOException;
import java.util.Scanner;

public class LinkList {
private static Scanner san = new Scanner(System.in);

public static void main(String[] args) throws IOException {
List list = new List();
for (int i = 1; i <= 10; i++) {
System.out.print("请输入第" + i + "个数: ");
list.add(san.nextInt());
list.print();
}
System.out.println("输入的数据如下: ");
list.print();
}
}

class node {
int data;
node next = this; // 指向自己
}

class List {
private node header = new node();

// 循环链表的尾部添加数据
public node add(int data) {
node current = new node();
node temp = header;
while (temp.next != header)
temp = temp.next;
current.data = data;
current.next = temp.next;
temp.next = current;
return current;
}

// 查询某个数字的位置 如果不在 返回-1;
public int search(int data) {
node temp = header;

int n = 0;
while (temp.next != header) {
temp = temp.next;
n++;
if (temp.data == data)
break;
}
if (temp.data == data)
return n;
else
return -1;
}

// 打印出整个链表
public void print() {
node temp = header;
while (temp.next != header) {

temp = temp.next;
System.out.print(temp.data + " ");
}
System.out.println();
}

// 插入数据
public node Insert(int pos, int data) {
node temp = header;
node current = new node();
for (int i = 0; i < pos - 1; i++) {
if (temp.next != header) {
temp = temp.next;
} else
return null;
}
current.data = data;
if (temp.next != header) {
current.next = temp.next;
}
temp.next = current;
return current;
}

// 删除某个数据
public node del(int data) {
node temp = header;
node oldtemp = null;
node current = null;
while (temp.next != header) {
oldtemp = temp;
temp = temp.next;
if (temp.data == data) {
current = temp;
break;
}
}
if (current == header)
return null;
oldtemp.next = current.next;
return current;
}
}

❺ 关于数据结构(java)的一个代码

描述栈抽象数据类型的SStack接口的声明
public interfaceSStack<E> //栈接口
{
boolean isEmpty(); //判断是否空栈,若空栈返回true
boolean push(E element); //元素element入栈,若操作成功返回true
E pop(); //出栈,返回当前栈顶元素,若栈空返回null
E get(); //取栈顶元素值,未出栈,若栈空返回null
}
顺序栈类具体操作方法的声明:

importdataStructure.linearList.SStack;

public classSeqStack<E> implements SStack<E>
//顺序栈类

{
private Object value[]; //存储栈的数据元素
private int top; //top为栈顶元素下标

public SeqStack(int capacity) //构闷缺造指定容量的空栈
{
this.value = newObject[Math.abs(capacity)];
this.top=-1;
}
public SeqStack() //构造默此罩衫认容量的空栈
{
this(10);
}

public boolean isEmpty() //判断是否空森腔栈,若空栈返回true
{
return this.top==-1;
}

public boolean push(E element) //元素element入栈,若操作成功返回true
{
if (element==null)
return false; //空对象(null)不能入栈

if (this.top==value.length-1) //若栈满,则扩充容量
{
Object[] temp = this.value;
this.value = newObject[temp.length*2];
for (int i=0; i<temp.length;i++)
this.value[i] = temp[i];
}
this.top++;
this.value[this.top] = element;
return true;
}

public E pop() //出栈,返回当前栈顶元素,若栈空返回null
{
if (!isEmpty())
return (E)this.value[this.top--];
else
return null;
}

public E get() //取栈顶元素值,未出栈,栈顶元素未改变
{
if (!isEmpty())
return (E)this.value[this.top];
else
return null;
}

public String toString() //返回栈中各元素的字符串描述
{
String str="{";
if (this.top!=-1)
str +=this.value[this.top].toString();
for (int i=this.top-1; i>=0; i--)
str += ","+this.value[i].toString();
return str+"} ";
}
实例引用public static void main(String args[])
{
SeqStack<String> stack = newSeqStack<String>(20);
System.out.print("Push: ");
char ch='a';
for(int i=0;i<5;i++)
{
String str =(char)(ch+i)+"";
stack.push(str);
System.out.print(str+" ");
}
System.out.println("\n"+stack.toString());

System.out.print("Pop : ");
while(!stack.isEmpty()) //全部出栈
System.out.print(stack.pop().toString()+" ");
System.out.println();
}

❻ java数据结构 gui编程。效果图在下面。有大神会么跪求源代码!也不需要完全跟图一样。能实现功

importjava.awt.*;
importjava.awt.event.*;
importjavax.swing.*;
importjavax.swing.event.*;

publicclassDSExample{
publicstaticvoidmain(String[]args){
EventQueue.invokeLater(newRunnable(){
@Overridepublicvoidrun(){
finalJFrameframe=newJFrame("Java数据结构");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

finalBoxhbox=Box.createHorizontalBox();
finalJButtonstackButton=newJButton("堆栈");
finalJButtonqueueButton=newJButton("队列");
finalJButtonstringButton=newJButton("串");
finalJButtonrecurButton=newJButton("递归");
finalJButtontreeButton=newJButton("树");
finalJButtongraphButton=newJButton("图");
finalJButtonopButton=newJButton("数据操作");
finalJButtonexpButton=newJButton("表达式");
hbox.add(stackButton);
hbox.add(queueButton);
hbox.add(stringButton);
hbox.add(recurButton);
hbox.add(treeButton);
hbox.add(graphButton);
hbox.add(opButton);
hbox.add(expButton);

finalBoxvbox=Box.createVerticalBox();
vbox.add(newJLabel("代码区域"));
finalJTextAreacodeArea=newJTextArea();
vbox.add(newJScrollPane(codeArea));

finalJPanelopPanel=newJPanel(newGridLayout(4,2));
finalJButtonpushButton=newJButton("数据入队");
finalJButtonpopButton=newJButton("数据出队");
finalJButtonheadButton=newJButton("队头元素");
finalJButtontailButton=newJButton("队尾元素");
finalJTextFieldpushData=newJTextField();
finalJTextFieldpopData=newJTextField();
finalJTextFieldheadData=newJTextField();
finalJTextFieldtailData=newJTextField();
opPanel.add(pushData);
opPanel.add(pushButton);
opPanel.add(popData);
opPanel.add(popButton);
opPanel.add(headData);
opPanel.add(headButton);
opPanel.add(tailData);
opPanel.add(tailButton);
opPanel.setBorder(BorderFactory.createTitledBorder("操作"));

frame.add(hbox,BorderLayout.PAGE_START);
frame.add(opPanel,BorderLayout.LINE_END);
frame.add(vbox,BorderLayout.CENTER);
frame.pack();
frame.setVisible(true);
}
});
}
}

热点内容
http代理java 发布:2025-04-23 12:24:11 浏览:825
野外普通话ftp 发布:2025-04-23 12:22:35 浏览:453
饥荒安卓版的怎么进去 发布:2025-04-23 12:21:56 浏览:743
数据库开发入门 发布:2025-04-23 12:13:39 浏览:366
怎么在我的世界国际服中玩服务器 发布:2025-04-23 11:55:59 浏览:973
安卓sav存档怎么改成txt 发布:2025-04-23 11:55:26 浏览:866
密码修改在哪里改 发布:2025-04-23 11:46:48 浏览:787
楚税通里面登陆的密码是设置什么 发布:2025-04-23 11:43:34 浏览:118
复利系统源码 发布:2025-04-23 11:43:33 浏览:252
python微信公众号开发教程 发布:2025-04-23 11:32:22 浏览:429