我需要帮助将堆栈类链接到测试床类 import java.util.Random; 公共类试验台{ 公共静态void main(字符串a[]{ //创建数组 int[]数组=新的int[100]; 随机=新随机(); //更改克隆阵列的变量以供参考 int[]arr=cloneArray(数组); 对于(int i1=0;i1
从这里开始,我必须链接我的stack类,但我不知道如何将这两个类正确地组合在一起。我是编程新手,我的学校把我扔进了一个java类,这对我来说太高了,现在我被困了5周我需要帮助将堆栈类链接到测试床类 import java.util.Random; 公共类试验台{ 公共静态void main(字符串a[]{ //创建数组 int[]数组=新的int[100]; 随机=新随机(); //更改克隆阵列的变量以供参考 int[]arr=cloneArray(数组); 对于(int i1=0;i1,java,Java,从这里开始,我必须链接我的stack类,但我不知道如何将这两个类正确地组合在一起。我是编程新手,我的学校把我扔进了一个java类,这对我来说太高了,现在我被困了5周 import java.util.Random; public class TestBed { public static void main(String a[]) { // creating the array int[] array = new int[100]; Random random = n
import java.util.Random;
public class TestBed {
public static void main(String a[]) {
// creating the array
int[] array = new int[100];
Random random = new Random();
// changing the variable of my clone array for reference
int[] arr = cloneArray(array);
for (int i1 = 0; i1 < 100; i1++)
array[i1] = random.nextInt(100) + 1;
// print out of bubble sort before and after the sort
System.out
.println("***********************Bubble Sort ****************************");
arr = cloneArray(array);
System.out.println("Values Before the sort:\n");
printArray(arr);
System.out.println();
bubble_srt(arr);
System.out.print("Values after the sort:\n");
printArray(arr);
// print out of selection sort before and after the sort
System.out.println();
System.out
.println("********************Selection Sort*****************************");
System.out.println(" Selection Sort\n\n");
arr = cloneArray(array);
System.out.println("Values Before the sort:\n");
printArray(arr);
System.out.println();
selection_srt(arr);
System.out.print("Values after the sort:\n");
printArray(arr);
System.out.println();
Stack stack = new Stack();
}
public static int[] buildArray(int bound) {
return null;
}
// clone array as a data type
public static int[] cloneArray(int[] data) {
return (int[]) data.clone();
}
// print array as a data type
public static void printArray(int[] data) {
// while there are still numbers left in the array print out the next
// value
for (int i = 0; i < data.length; i++)
System.out.print(data[i] + " ");
}
// bubble syntax
public static void bubble_srt(int a[]) {
int t = 0;
for (int i = 0; i < a.length; i++) {
for (int j = 1; j < (a.length - i); j++) {
if (a[j - 1] > a[j]) {
t = a[j - 1];
a[j - 1] = a[j];
a[j] = t;
}
}
}
}
// selection syntax
public static void selection_srt(int array[]) {
for (int x = 0; x < array.length; x++) {
int index_of_min = x;
for (int y = x; y < array.length; y++) {
if (array[index_of_min] > array[y]) {
index_of_min = y;
}
}
int temp = array[x];
array[x] = array[index_of_min];
array[index_of_min] = temp;
}
}
}
公共类堆栈{
节点顶部;
整数大小;
公共堆栈(){
top=null;
尺寸=0;
}
公共int-pop(){
如果(顶部!=null){
int item=top.data;
top=top.next;
大小--;
退货项目;
}
返回-1;
}
公共无效推送(整数数据){
节点t=新节点(数据);
t、 next=this.top;
this.top=t;
大小++;
}
公共布尔值为空(){
返回大小(0){
系统输出打印项次(“位置:+pos+”元素:+n.data);
如果(位置>0){
n=n.next;
}
pos--;
}
}
}
类节点{
公共int数据;
公共节点下一步;
节点(int d){
数据=d;
next=null;
}
公共int getData(){
返回数据;`在这里输入代码`
}`在这里输入代码`
{
堆栈s=新堆栈();
s、 推(9);
s、 推(2);
s、 推(7);
s、 推(3);
s、 推(6);
s、 推(4);
s、 推(5);
System.out.println(“大小为:+s.getSize());
//s.printStack();
int size=s.getSize();
对于(int i=0;i
在堆栈类文件的开头,创建一个包名,例如:
public class Stack {
Node top;
int size;
public Stack() {
top = null;
size = 0;
}
public int pop() {
if (top != null) {
int item = top.data;
top = top.next;
size--;
return item;
}
return -1;
}
public void push(int data) {
Node t = new Node(data);
t.next = this.top;
this.top = t;
size++;
}
public boolean isEmpty() {
return size <= 0;
}
public int getSize() {
return size;
}
public int peek() {
return top.data;
}
public void printStack() {
Node n = this.top;
int pos = this.getSize();
while (pos > 0) {
System.out.println("Position: " + pos + " Element: " + n.data);
if (pos > 0) {
n = n.next;
}
pos--;
}
}
}
class Node {
public int data;
public Node next;
Node(int d) {
data = d;
next = null;
}
public int getData() {
return data;`enter code here`
}`enter code here`
{
Stack s = new Stack();
s.push(9);
s.push(2);
s.push(7);
s.push(3);
s.push(6);
s.push(4);
s.push(5);
System.out.println("Size is: " + s.getSize());
// s.printStack();
int size = s.getSize();
for (int i = 0; i < size; i++) {
System.out.print(s.pop() + " ");
}
}
}
然后在测试床类文件中,从包中导入堆栈类,例如:
package com.example.testcode;
你说的链接是什么意思?还有,你试过什么了?
import com.example.testcode.Stack;