java数据结构编程题(用java编写一个数据结构的题!)

本文目录
- 用java编写一个数据结构的题!
- java中setResult()是什么作用
- java数据结构课程设计病人排队就诊问题!!!
- java里面什么数据结构可以从value查找key,算法复杂度为O(1)
- JAVA数据结构
- 【Java数据结构马踏棋盘问题】将马随机放在国际象棋的8×8棋盘Board[8][8]的某个方格中
- 求数据结构(JAVA版)实验树和二叉树题目答案
- 一道java编程题,数据结构
用java编写一个数据结构的题!
线性表跟是不是数组没关系啊。。。栈和队列都是线性表吧。。不太懂你的意思。。
public class SeqList {
public static void main(String args) {
int{1,2,3,4,5,6,7};
int{3,5,8,9};
int;
new SeqList().seqListMerge(a, b, c);
}
public void seqListMerge(int c){
//i为数组a的计数器
int i = 0;
//j为数组b的计数器
int j = 0;
//k为数组c的计数器
int k = 0;
//判断两个数组长度,当一个先用完的时候推出循环
while(i 《 a.length && j 《 b.length){
if(a){
c;
k++;
j++;
}else{
c;
k++;
i++;
}
}
//如果a数组先到结尾,那么把b数组的剩下的值拼到c里
if( i == a.length){
while(j 《 b.length){
c;
k++;
j++;
}
}
//如果b数组先到结尾,那么把a数组的剩下的值拼到c里
if(j == b.length){
while(i 《 a.length){
c;
k++;
i++;
}
}
for(int p : c){
System.out.println(p);
}
}
}
java中setResult()是什么作用
现在有两个activity
A和B,假如我们从A跳转到B,再B完成相应的工作以后finish掉B,然后传数据给A,A在接受到数据以后做相应的操作。我们可以使用三个函数:
A:
//启动B
Intent intent = new Intent();
intent.setClass(A.this, B.class);
startActivityForResult(intent,
2);//2是我们自己定义常量,对应下面使用到的resultCode
B:
//do something
setResult(2, null);
finish();
以上主要代码完成后我们需要在A中override onActivityResult()函数:
A:
@Override
protected void onActivityResult(int requestCode, int resultCode,
Intent data) {
super.onActivityResult(requestCode, resultCode,
data);
if (2 == requestCode)
{
//do
something
}
}
java数据结构课程设计病人排队就诊问题!!!
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Deque;
import java.util.List;
import java.util.Stack;
public class DoctorMain implements Runnable
{
private static Deque《Integer》 binRenDeque;
private boolean onWork = false;
private boolean isDoctor = true;
public static Integer binRenNumber = 0;
public DoctorMain()
{
System.out.println("开始上班");
binRenDeque = new ArrayDeque《Integer》();
}
/**
* 《br/》
* 《方法概述》 《br/》
* 《方法详细概述》 《br/》
* 《版本》 《br/》
* 《作者》 *
* @param args
*/
public static void main(String args)
{
// TODO Auto-generated method stub
DoctorMain doctor=new DoctorMain();
doctor.setDoctor(true);
//上班了
doctor.setOnWork(true);
Thread th1=new Thread(doctor);
DoctorMain binRen=new DoctorMain();
binRen.setDoctor(false);
binRen.setOnWork(true);
Thread th3=new Thread(binRen);
th1.start();
th3.start();
try
{
Thread.sleep(60000);
doctor.setOnWork(false);
binRen.setOnWork(false);
}
catch (InterruptedException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@Override
public void run()
{
while (onWork)
{
try
{
//是医生还是病人
if (isDoctor)
{
// 医生给病人看病时间
Thread.sleep(2000);
if (!binRenDeque.isEmpty())
{
Integer number = binRenDeque.pollLast();
System.out.println("医生正在给" + number + "号病人看病");
}
}
else
{
//病人来的间隔时间
Thread.sleep((int)(Math.random()*3000));
binRenNumber++;
System.out.println("来了一个病人,号码是:"+binRenNumber);
binRenDeque.push(binRenNumber);
}
//列出所有等待的病人
for(Integer bn:binRenDeque)
{
System.out.println(bn+"号的病人在排队");
}
}
catch (InterruptedException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
System.out.println("医生下班了");
}
public static Deque《Integer》 getBinRenStack()
{
return binRenDeque;
}
public static void setBinRenStack(Deque《Integer》 binRenStack)
{
DoctorMain.binRenDeque = binRenStack;
}
public boolean isOnWork()
{
return onWork;
}
public void setOnWork(boolean onWork)
{
this.onWork = onWork;
}
public boolean isDoctor()
{
return isDoctor;
}
public void setDoctor(boolean isDoctor)
{
this.isDoctor = isDoctor;
}
public static Integer getBinRenNumber()
{
return binRenNumber;
}
public static void setBinRenNumber(Integer binRenNumber)
{
DoctorMain.binRenNumber = binRenNumber;
}
}
java里面什么数据结构可以从value查找key,算法复杂度为O(1)
O(1)啊?
public V get(Object key) {
if (key == null)
return getForNullKey();
int hash = hash(key.hashCode());
for (Entry《K,V》 e = table;
e != null;
e = e.next) {
Object k;
if (e.hash == hash && ((k = e.key) == key || key.equals(k)))
return e.value;
}
return null;
}
这是HashMap的get方法.
key找Vaule都不是O(1)
别以为
a=Map.get(XXX) 就是O(1)
只是它把找的详细过程写在get里面了.
JAVA数据结构
public class CallNotes {
String name;
String phoneNumber;
static Map《String,String》 noteMap=null;
static List《CallLog》 logList=null;
CallNotes(){
this.noteMap = new HashMap《String,String》();
this.logList = new ArrayList《CallLog》();
}
void addRecord(String name,String number){
noteMap.put(name,number);
}
void removeRecord(String name){
noteMap.remove(name);
}
String searchPhoneNumber(String name){
String number=null;
number = noteMap.get(name);
logList.add(new CallLog(name, number, new Date()));
return number;
}
void outputCallLog(CallLog log){
if(log==null) return ;
log.outputLog();
}
class CallLog{
String name;
String number;
Date date;
CallLog(String name,String num,Date date) {
this.name=name;
this.number=num;
this.date=date;
}
void outputLog(){
System.out.println("name:"+this.name);
System.out.println("number:"+this.number);
System.out.println("date:"+this.date);
}
}
}
用HashMap实现可以吗?
【Java数据结构马踏棋盘问题】将马随机放在国际象棋的8×8棋盘Board[8][8]的某个方格中
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Random;
import java.util.Stack;
public class T {
private static final int { { -2, 1 }, { -1, 2 },
{ 1, 2 }, { 2, 1 }, { 2, -1 }, { 1, -2 }, { -1, -2 }, { -2, -1 } };
private static final int SIZE = 8;
private static final int BASE = SIZE + 4;
private static int board;
private static NeighborComparator neighborComparator = new NeighborComparator();
public static void main(String args) {
board = new int;
for (int r = 0; r 《 BASE; r++) {
for (int c = 0; c 《 BASE; c++) {
if (r 《 2 || r 》 BASE - 3 || c 《 2 || c 》 BASE - 3) {
board = -1;
}
}
}
int row = 2 + new Random().nextInt(SIZE);
int col = 2 + new Random().nextInt(SIZE);
solve(row, col);
}
private static void solve(int r, int c) {
Stack《Cell》 stack = new Stack《Cell》();
int count = 1;
Cell cell = new Cell(r, c, neighbors(r, c));
stack.push(cell);
board = count++;
while (!stack.isEmpty()) {
if (stack.size() == SIZE * SIZE) {
break;
}
cell = stack.peek();
if (cell.nextNeighbor 《 cell.neighbors.size()) {
int neighbor = cell.neighbors.get(cell.nextNeighbor);
r = neighbor;
c = neighbor;
board = count++;
stack.push(new Cell(r, c, neighbors(r, c)));
cell.nextNeighbor++;
} else {
stack.pop();
board = 0;
count--;
}
}
if (stack.size() == SIZE * SIZE) {
print();
} else {
System.out.println("无解");
}
}
private static class NeighborComparator implements Comparator《int》 {
public int compare(int b) {
return a;
}
}
private static List《int》 neighbors(int r, int c) {
List《int》 neighbors = new ArrayList《》();
for (int m : MOVES) {
int x = m;
int y = m;
if (board == 0) {
neighbors.add(new int { r + y, c + x, countNeighbors(r + y, c + x) });
}
}
Collections.sort(neighbors, neighborComparator);
return neighbors;
}
private static int countNeighbors(int r, int c) {
int num = 0;
for (int m : MOVES) {
if (board == 0) {
num++;
}
}
return num;
}
private static void print() {
for (int i = 2; i 《 board.length - 2; i++) {
for (int j = 2; j 《 board.length - 2; j++) {
System.out.printf("%2d ", board);
}
System.out.println();
}
System.out.println();
}
private static class Cell {
int r;
int c;
List《int》 neighbors;
int nextNeighbor = 0;
public Cell(int r, int c, List《int》 neighbors) {
this.r = r;
this.c = c;
this.neighbors = neighbors;
}
}
}
求数据结构(JAVA版)实验树和二叉树题目答案
/**
* @param args
之前在大学的时候写的一个二叉树算法,运行应该没有问题,就看适不适合你的项目了 */
public static void main(String args) {
BiTree e = new BiTree(5);
BiTree g = new BiTree(7);
BiTree h = new BiTree(8);
BiTree l = new BiTree(12);
BiTree m = new BiTree(13);
BiTree n = new BiTree(14);
BiTree k = new BiTree(11, n, null);
BiTree j = new BiTree(10, l, m);
BiTree i = new BiTree(9, j, k);
BiTree d = new BiTree(4, null, g);
BiTree f = new BiTree(6, h, i);
BiTree b = new BiTree(2, d, e);
BiTree c = new BiTree(3, f, null);
BiTree tree = new BiTree(1, b, c);
System.out.println("递归前序遍历二叉树结果: ");
tree.preOrder(tree);
System.out.println();
System.out.println("非递归前序遍历二叉树结果: ");
tree.iterativePreOrder(tree);
System.out.println();
System.out.println("递归中序遍历二叉树的结果为:");
tree.inOrder(tree);
System.out.println();
System.out.println("非递归中序遍历二叉树的结果为:");
tree.iterativeInOrder(tree);
System.out.println();
System.out.println("递归后序遍历二叉树的结果为:");
tree.postOrder(tree);
System.out.println();
System.out.println("非递归后序遍历二叉树的结果为:");
tree.iterativePostOrder(tree);
System.out.println();
System.out.println("层次遍历二叉树结果: ");
tree.LayerOrder(tree);
System.out.println();
System.out.println("递归求二叉树中所有结点的和为:"+getSumByRecursion(tree));
System.out.println("非递归求二叉树中所有结点的和为:"+getSumByNoRecursion(tree));
System.out.println("二叉树中,每个节点所在的层数为:");
for (int p = 1; p 《= 14; p++)
System.out.println(p + "所在的层为:" + tree.level(p));
System.out.println("二叉树的高度为:" + height(tree));
System.out.println("二叉树中节点总数为:" + nodes(tree));
System.out.println("二叉树中叶子节点总数为:" + leaf(tree));
System.out.println("二叉树中父节点总数为:" + fatherNodes(tree));
System.out.println("二叉树中只拥有一个孩子的父节点数:" + oneChildFather(tree));
System.out.println("二叉树中只拥有左孩子的父节点总数:" + leftChildFather(tree));
System.out.println("二叉树中只拥有右孩子的父节点总数:" + rightChildFather(tree));
System.out.println("二叉树中同时拥有两个孩子的父节点个数为:" + doubleChildFather(tree));
System.out.println("--------------------------------------");
tree.exChange();
System.out.println("交换每个节点的左右孩子节点后......");
System.out.println("递归前序遍历二叉树结果: ");
tree.preOrder(tree);
System.out.println();
System.out.println("非递归前序遍历二叉树结果: ");
tree.iterativePreOrder(tree);
System.out.println();
System.out.println("递归中序遍历二叉树的结果为:");
tree.inOrder(tree);
System.out.println();
System.out.println("非递归中序遍历二叉树的结果为:");
tree.iterativeInOrder(tree);
System.out.println();
System.out.println("递归后序遍历二叉树的结果为:");
tree.postOrder(tree);
System.out.println();
System.out.println("非递归后序遍历二叉树的结果为:");
tree.iterativePostOrder(tree);
System.out.println();
System.out.println("层次遍历二叉树结果: ");
tree.LayerOrder(tree);
System.out.println();
System.out.println("递归求二叉树中所有结点的和为:"+getSumByRecursion(tree));
System.out.println("非递归求二叉树中所有结点的和为:"+getSumByNoRecursion(tree));
System.out.println("二叉树中,每个节点所在的层数为:");
for (int p = 1; p 《= 14; p++)
System.out.println(p + "所在的层为:" + tree.level(p));
System.out.println("二叉树的高度为:" + height(tree));
System.out.println("二叉树中节点总数为:" + nodes(tree));
System.out.println("二叉树中叶子节点总数为:" + leaf(tree));
System.out.println("二叉树中父节点总数为:" + fatherNodes(tree));
System.out.println("二叉树中只拥有一个孩子的父节点数:" + oneChildFather(tree));
System.out.println("二叉树中只拥有左孩子的父节点总数:" + leftChildFather(tree));
System.out.println("二叉树中只拥有右孩子的父节点总数:" + rightChildFather(tree));
System.out.println("二叉树中同时拥有两个孩子的父节点个数为:" + doubleChildFather(tree));
}
}
一道java编程题,数据结构
/**
* 四则运算表达式计算
* @author penli
*
*/
public class Arithmetic {
public static void main(String args){
System.out.println(arithmetic("2.2+((3+4)*2-22)/2*3.2"));
}
public static double arithmetic(String exp){
String result = parseExp(exp).replaceAll("", "");
return Double.parseDouble(result);
}
/**
* 解析计算四则运算表达式,例:2+((3+4)*2-22)/2*3
* @param expression
* @return
*/
public static String parseExp(String expression){
//String numberReg="^((?!0)\\d+(\\.\\d+(?《!0))?)|(0\\.\\d+(?《!0))$";
expression=expression.replaceAll("\\s+", "").replaceAll("^\\((.+)\\)$", "$1");
String checkExp="\\d";
String minExp="^((\\d+(\\.\\d+)?)|(\\))$";
//最小表达式计算
if(expression.matches(minExp)){
String result=calculate(expression);
return Double.parseDouble(result)》=0?result:"";
}
//计算不带括号的四则运算
String noParentheses="^+$";
String priorOperatorExp="(((\\d+(\\.\\d+)?)|(\\)))";
String operatorExp="(((\\d+(\\.\\d+)?)|(\\)))";
if(expression.matches(noParentheses)){
Pattern patt=Pattern.compile(priorOperatorExp);
Matcher mat=patt.matcher(expression);
if(mat.find()){
String tempMinExp=mat.group();
expression=expression.replaceFirst(priorOperatorExp, parseExp(tempMinExp));
}else{
patt=Pattern.compile(operatorExp);
mat=patt.matcher(expression);
if(mat.find()){
String tempMinExp=mat.group();
expression=expression.replaceFirst(operatorExp, parseExp(tempMinExp));
}
}
return parseExp(expression);
}
//计算带括号的四则运算
String minParentheses="\\(+\\)";
Pattern patt=Pattern.compile(minParentheses);
Matcher mat=patt.matcher(expression);
if(mat.find()){
String tempMinExp=mat.group();
expression=expression.replaceFirst(minParentheses, parseExp(tempMinExp));
}
return parseExp(expression);
}
/**
* 计算最小单位四则运算表达式(两个数字)
* @param exp
* @return
*/
public static String calculate(String exp){
exp=exp.replaceAll("", "");
String number", "$1,").split(",");
BigDecimal number1=new BigDecimal(number);
BigDecimal number2=new BigDecimal(number);
BigDecimal result=null;
String operator=exp.replaceFirst("^.*\\d().+$", "$1");
if("+".equals(operator)){
result=number1.add(number2);
}else if("-".equals(operator)){
result=number1.subtract(number2);
}else if("*".equals(operator)){
result=number1.multiply(number2);
}else if("/".equals(operator)){
result=number1.divide(number2);
}
return result!=null?result.toString():null;
}
}

更多文章:
kali上传文件到apache服务器(如何在macOS系统和KaliLinux系统之间共享文件)
2025年10月25日 23:30
c语言一维数组有几个数(c语言的一维数组中至少要有几个元素)
2025年11月27日 13:30
织梦岛隐藏要素(塞尔达传说织梦岛脸孔神殿迷宫怎么通关 脸孔神殿迷宫)
2025年9月18日 18:45
最新版android studio安装(如何安装android studio)
2025年9月24日 10:15
97的十六进制(97 98 换成八进制 如何换 如何将十进制转换成八进制或者十六进制 哪位大仙帮帮忙)
2026年4月5日 15:00
redis命令登录(软件Xshell连接Redis操作教程)
2026年6月6日 17:30
category nouns(the different lexical category in translation)
2026年1月9日 10:45
哈夫曼树编码不唯一那解码结果(如何解决哈夫曼树不唯一的问题)
2025年6月24日 02:45
关系数据库管理系统有哪些?(下面哪些属于关系型数据库管理系统())
2026年5月2日 12:15
insertrow(在Power Query中如何插入自定义行)
2025年10月10日 12:30
















