首页 / JAVA / java_数据结构_1
java_数据结构_1
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了java_数据结构_1,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含4537字,纯文字阅读大概需要7分钟。
内容图文
![java_数据结构_1](/upload/InfoBanner/zyjiaocheng/613/0a24be4c475e4d2eadcb0b921828cd4c.jpg)
1.稀疏矩阵
2.队列
文章目录
1.稀疏数组
ublic class demo01 {
public static void main(String[] args) {
int chew[][] = new int[11][11];
chew[1][1] = 21;
chew[2][1] = 1;
for(int[] row : chew) {
for(int data : row) {
System.out.print(data+ "\t");
}
System.out.println();
}
int sum = 0;
for(int i = 0;i < chew.length;i++) {
for(int j = 0;j < chew[i].length;j++) {
if(chew[i][j] != 0) {
sum++;
}
}
}
System.out.println("sum = "+ sum);
int sparseArray[][] = new int[sum + 1][3];
sparseArray[0][0] = 11;
sparseArray[0][1] = 11;
sparseArray[0][2] = sum;
int count = 0;
for(int i = 0;i < chew.length;i++) {
for(int j = 0;j < chew[i].length;j++) {
if(chew[i][j]!=0) {
count++;
sparseArray[count][0] = i;
sparseArray[count][1] = j;
sparseArray[count][2] = chew[i][j];
}
}
}
for(int i = 0;i < sparseArray.length;i++) {
System.out.printf("%d\t%d\t%d\t\n",sparseArray[i][0],sparseArray[i][1],sparseArray[i][2]);
}
System.out.println();
int chew2[][] = new int[sparseArray[0][0]][sparseArray[0][1]];
for(int i = 1;i < sparseArray.length;i++) {
chew2[sparseArray[i][0]][sparseArray[i][1]] = sparseArray[i][2];
}
for(int[] row : chew) {
for(int data : row) {
System.out.print(data+ "\t");
}
System.out.println();
}
}
}
2.队列-单向
import java.util.Scanner;
public class demo01 {
public static void main(String[] args) {
ArrayQueue q = new ArrayQueue(3);
char key = ' ';
Scanner scan = new Scanner(System.in);
boolean loop = true;
while(loop) {
System.out.println("s(show):显示队列");
System.out.println("a(add):添加队列");
System.out.println("g(get):得到数据");
System.out.println("h(head):头部数据");
System.out.println("e(exit):退出");
key = scan.next().charAt(0);
switch(key) {
case 's':
q.showQ();
break;
case 'a':
int value = scan.nextInt();
q.addQ(value);
break;
case 'g':
try {
int res = q.getq();
System.out.println(res);
} catch (Exception e) {
System.out.println(e.getMessage());
// TODO: handle exception
}
break;
case 'h':
try {
int res = q.headQ();
System.out.println(res);
} catch (Exception e) {
System.out.println(e.getMessage());
// TODO: handle exception
}
break;
case 'e':
scan.close();
loop = false;
break;
}
}
}
}
class ArrayQueue{
private int max;
private int front;
private int rear;
private int[] arr;//存放的数据,模拟的队列
public ArrayQueue(int maxsize) {
max = maxsize;
arr = new int[max];
front = -1;
rear = -1;
}
public boolean isFull() {
return rear == max - 1;
}
public boolean isE() {
return rear == front;
}
public void addQ(int n) {
if(isFull()) {
System.out.println("队列满");
}else {
rear++;
arr[rear] = n;
}
}
public int getq() {
if(isE()) {
throw new RuntimeException("异常");
}
front++;
return arr[front];
}
public void showQ() {
if(isE()) {
System.out.println("空");
return;
}
for(int i = 0;i < arr.length;i++) {
System.out.printf("arr[%d] = %d\n",i,arr[i]);
}
}
public int headQ() {
if(isE()) {
throw new RuntimeException("空");
}
return arr[front + 1];
}
}
单向会存在假溢出的问题。
3.队列循环
import java.util.Scanner;
public class demo03 {
public static void main(String[] args) {
ArrayQueue1 q = new ArrayQueue1(3);
char key = ' ';
Scanner scan = new Scanner(System.in);
boolean loop = true;
while(loop) {
System.out.println("s(show):显示队列");
System.out.println("a(add):添加队列");
System.out.println("g(get):得到数据");
System.out.println("h(head):头部数据");
System.out.println("e(exit):退出");
key = scan.next().charAt(0);
switch(key) {
case 's':
q.showQ();
break;
case 'a':
int value = scan.nextInt();
q.addQ(value);
break;
case 'g':
try {
int res = q.getq();
System.out.println(res);
} catch (Exception e) {
System.out.println(e.getMessage());
// TODO: handle exception
}
break;
case 'h':
try {
int res = q.headQ();
System.out.println(res);
} catch (Exception e) {
System.out.println(e.getMessage());
// TODO: handle exception
}
break;
case 'e':
scan.close();
loop = false;
break;
}
}
}
}
class ArrayQueue1{
private int max;
private int front;
private int rear;
private int[] arr;//存放的数据,模拟的队列
public ArrayQueue1(int maxsize) {
max = maxsize;
arr = new int[max];
}
public boolean isFull() {
return (rear + 1) % max == front;
}
public boolean isE() {
return rear == front;
}
public void addQ(int n) {
if(isFull()) {
System.out.println("队列满");
return;
}
arr[rear] = n;
rear = (rear + 1)%max;
}
public int getq() {
if(isE()) {
throw new RuntimeException("异常");
}
int value = arr[front];
front = (front + 1) % max;
return value;
}
public void showQ() {
if(isE()) {
System.out.println("空");
return;
}
for(int i = front;i < front + size();i++) {
System.out.printf("arr[%d] = %d\n",i%max,arr[i%max]);
}
}
public int size() {
return (rear + max - front) % max;
}
public int headQ() {
if(isE()) {
throw new RuntimeException("空");
}
return arr[front];
}
}
注意单向与循环的区别:
循环的一些语句要带有%maxsize
内容总结
以上是互联网集市为您收集整理的java_数据结构_1全部内容,希望文章能够帮你解决java_数据结构_1所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。