Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/348.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在堆栈中获得相等的值_Java_Data Structures_Stack - Fatal编程技术网

Java 在堆栈中获得相等的值

Java 在堆栈中获得相等的值,java,data-structures,stack,Java,Data Structures,Stack,我在java上做了一个堆栈,它包含五个整数,但我必须打印出来,只有值相等 例1-2-2-3-4 同样的数字是:2 我如何确定哪些号码相同? 这是我的密码: package e.d_pilas; import java.util.*; public class ED_PILAS { private int stck[]; private int tos; ED_PILAS(int size){ //New stack stck = ne

我在java上做了一个堆栈,它包含五个整数,但我必须打印出来,只有值相等

例1-2-2-3-4 同样的数字是:2

我如何确定哪些号码相同?

这是我的密码:

package e.d_pilas;

import java.util.*;

public class ED_PILAS {
    private int stck[];
    private int tos;

    ED_PILAS(int size){
        //New stack
        stck = new int[size];
        tos = -1;
    }

    void push(int value) {
        stck[++tos] = value;
    }

    int pop() {
        if (tos < 0) {
            return 0;
        } else
            return stck[tos--];
    }

    public static void main(String[] args) {
        int number;
        Scanner read = new Scanner (System.in);
        System.out.print("Enter five (5) numbers to fill the stack \n");
        ED_PILAS stack = new ED_PILAS(5);

        for (int i = 1; i < 6; i++){
            System.out.print("Enter the value "+i+" to fill the stack \n");
            number=read.nextInt();
            stack.push(number); 
        }
        System.out.println("Equal values contained in the stack: \n");

        for (int j = 1; j < 6; j++){
            System.out.println("\t " + stack.pop());    
        } 
    }
}
e.d_pilas包装;
导入java.util.*;
公共级ED_PILAS{
私人int stck[];
私人int tos;
ED_PILAS(内部尺寸){
//新堆栈
stck=新整数[大小];
tos=-1;
}
无效推送(int值){
stck[++tos]=值;
}
int-pop(){
如果(tos<0){
返回0;
}否则
返回stck[tos--];
}
公共静态void main(字符串[]args){
整数;
扫描仪读取=新扫描仪(System.in);
System.out.print(“输入五(5)个数字以填充堆栈\n”);
ED_PILAS stack=新ED_PILAS(5);
对于(int i=1;i<6;i++){
System.out.print(“输入值“+i+”以填充堆栈\n”);
number=read.nextInt();
堆栈推送(数字);
}
System.out.println(“堆栈中包含的相等值:\n”);
对于(int j=1;j<6;j++){
System.out.println(“\t”+stack.pop());
} 
}
}

谢谢大家!

当您从堆栈执行
pop()
操作时,您可以将其存储在
ArrayList
中,并检查每个
pop()
是否已经存在。如果重复,则将其打印并标记为已打印(以便您不再打印)。

在第一种方法中,它将只打印重复项一次。在第二种方法中,若堆栈包含两个以上的条目,它将打印多次

        ArrayList<int> list=new ArrayList<int>();

        for (int j = 1; j < 6; j++){
            int num=stack.pop();
            if(list.contains(num)){
               System.out.println(num);
            }
            else{
                 list.add(num);
            }

        } 

使用此逻辑,它将适用于连续的相同值,否则使用ArrayList:

int n=stack.pop();
int假人;
对于(int j=1;j<6;j++){
dummy=stack.pop();
如果(n==虚拟)
{
System.out.println(“\t相同的数字为”+n);
}
否则{
n=假人;

}

使用ArrayList存储以前的pop值。如果在pop过程中再次看到此值,只需打印数字即可

ArrayList<Integer> popdList=new ArrayList<Integer>();

for (int j = 1; j < 6; j++){
    int value=stack.pop();
    if(popdList.contains(value){
        System.out.println(value);
     } else{
         popdList.add(value);
     }
} 
ArrayList popdList=new ArrayList();
对于(int j=1;j<6;j++){
int value=stack.pop();
if(popdList.contains)(值){
系统输出打印项次(值);
}否则{
添加(值);
}
} 

此外,您可以编辑pop函数以仅返回重复值,而不是更改主函数,如下所示:

ArrayList<Integer> popdList=new ArrayList<Integer>();

int pop() {
    if (tos < 0) {
        return 0;
    } else {
        int value = stck[tos--];
        if(popdList.contains(value){
            return value;
        } else{
            popdList.add(value);
        }
    }
}
ArrayList popdList=new ArrayList();
int-pop(){
如果(tos<0){
返回0;
}否则{
int值=stck[tos--];
if(popdList.contains)(值){
返回值;
}否则{
添加(值);
}
}
}

如果有多个相同的数字怎么办?@SarthakMittal也会显示,只要它们超过两个就可以了。是否真的需要使用Stack?@SarthakMittal是的,这是工作的一部分,因为您已经发布了工作答案,使用Stack解决此类问题效率很低:)该方法是逻辑性最强、实现最简单的方法。
ArrayList<Integer> popdList=new ArrayList<Integer>();

int pop() {
    if (tos < 0) {
        return 0;
    } else {
        int value = stck[tos--];
        if(popdList.contains(value){
            return value;
        } else{
            popdList.add(value);
        }
    }
}