Java 查找数组中元素前后的元素引用

Java 查找数组中元素前后的元素引用,java,arrays,Java,Arrays,我正在写一个算法: 在此算法中,如果数组包含元素3,则元素3不能位于两个1元素之间,如下所示: int-array={5,2,10,'3',15,'1',2,2}//符号仅用于突出显示所讨论的元素 上面的数组包含元素3,注意在3之前没有元素1。 但是在元素3之后有一个元素1,在这种情况下,它应该返回True。 它应该返回true,因为元素3没有被两个元素1包围 int-array={'3',2,18,>'1',0,3,-11,'1'

我正在写一个算法:

在此算法中,如果数组包含元素3,则元素3不能位于两个1元素之间,如下所示:

int-array={5,2,10,'3',15,'1',2,2}//符号仅用于突出显示所讨论的元素

上面的数组包含元素3,注意在3之前没有元素1。 但是在元素3之后有一个元素1,在这种情况下,它应该返回True。 它应该返回true,因为元素3没有被两个元素1包围


int-array={'3',2,18,>'1',0,3,-11,'1'<,'3'}/',在数组上迭代怎么样

List<Integer> nums = new ArrayList<>();
List<Integer> target = new ArrayList<>(Arrays.asList(new int[]{1, 3, 1}));
for (int i : array) {
    if (i == 3 || i == 1) {
        nums.add(i);
    }
}
return Collections.indexOfSubList(nums, target) >= 0;

}

根据您的问题,如果您只是想检查3是否被1包围,您可以这样做。这是根据您的问题假设您的阵列中没有多个3

public static void main(String[] args) 
{
    int[] array = {3,1,2,4,3,1,3,1};
    int posOfThree = 0;
    int posOfFirstThree = 0;
    boolean noLeft = true;
    boolean noRight = true;
    boolean firstThreeFound = false;
    //Get position of 3
    for (int x=0; x<array.length; x++) 
    {
        if (array[x] == 3)
        {
            if (firstThreeFound == false)
            {   
                posOfFirstThree = x; //Position of the first occurred 3
                firstThreeFound = true;     
            }       
            posOfThree = x;          //Position of the last occurred 3
        }       
    }   

    //Check if there is 1 on left hand side of first 3
    int left = 0;
    while (posOfFirstThree - left > 0)  
    {
        left ++;
        if (array[posOfFirstThree-left] == 1)
            noLeft = false;
    }   

    //Check if there is 1 on right hand side of last 3
    int right = 0;
    while (posOfThree + right < array.length-1) 
    {
        right ++;
        if (array[posOfThree + right] == 1)
            noRight = false;
    }

    System.out.println("Outcome: " + (noLeft || noRight));
}
程序输出:结果:True

因为您只能使用普通循环和简单数组来执行此操作。这非常简单,可以满足您的需求。当然,我相信这个解决方案可以进一步改进,但是,即使增加或减少数组大小,这段代码也可以完全满足您的需要


编辑:编辑后的版本可以处理数组中的多个3,并检查是否所有3都被1包围。

您到底想要什么?定义清楚,再次阅读我的问题我已经阅读了您的问题20次,我仍然没有得到您想要的数组中3之间不应该有两个1。例如,如果数组是{5,2,10,3,15,1,2,2},它应该返回true。因为3之后只有一个1,3之前没有1。当有多个3时会发生什么?添加一个正则表达式替代项,但为什么不呢?如果它在数组中包含多个3,像int数组={3,2,18,1,0,3,-11,1,3};那么什么条件必须改变呢?那取决于你想要什么。你只想检查中间的3是否被1包围,还是需要检查所有的3是否都被1包围。我想检查没有3被1包围,如果它被1包围,它应该返回false。你是说你想检查数组中的每个3?这很简单。您只需检查第一次遇到的3和最后一次遇到的3。如果第一个和最后3个被包围,那么中间的所有3个肯定也被包围。
int test[] = {1, 5, 3, 2, 5, 67, 8, 1};
return Arrays.toString(test).matches(".*1.*3.*1.*"); //greedy search
 public class Third {  
 public static void main(String[] args){  
    int[] array = {1,2,4,3, 1}; 

    for(int i=0;i<array.length;i++)
    {

        if(array[i]==3)
        {
            for(int j=0;j<=i;j++)
            {
                if(array[j]==1)
                {

                     System.out.println("I foud One before "+array[j]); 


                }else
                {
                    break;
                }


            System.out.println("yes i found the array:"+array[i]);

        }

            for(int z=i;z>array.length;z++)
            {
                if(array[z]==1)
                {
                    System.out.println("I found after 3 is :"+array[z]);
                }
                break;
            }
    }
public static void main(String[] args) 
{
    int[] array = {3,1,2,4,3,1,3,1};
    int posOfThree = 0;
    int posOfFirstThree = 0;
    boolean noLeft = true;
    boolean noRight = true;
    boolean firstThreeFound = false;
    //Get position of 3
    for (int x=0; x<array.length; x++) 
    {
        if (array[x] == 3)
        {
            if (firstThreeFound == false)
            {   
                posOfFirstThree = x; //Position of the first occurred 3
                firstThreeFound = true;     
            }       
            posOfThree = x;          //Position of the last occurred 3
        }       
    }   

    //Check if there is 1 on left hand side of first 3
    int left = 0;
    while (posOfFirstThree - left > 0)  
    {
        left ++;
        if (array[posOfFirstThree-left] == 1)
            noLeft = false;
    }   

    //Check if there is 1 on right hand side of last 3
    int right = 0;
    while (posOfThree + right < array.length-1) 
    {
        right ++;
        if (array[posOfThree + right] == 1)
            noRight = false;
    }

    System.out.println("Outcome: " + (noLeft || noRight));
}