Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7413519
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T06:55:21+00:00 2026-05-29T06:55:21+00:00

I want to write a method that when supplied an array of ints will

  • 0

I want to write a method that when supplied an array of ints will do the following. For each pair of array elements it will combine them and put them into a list of an inner class objects. Then it will compare each element in the array and check if it will fit between each pair values. (i.e. I have an array 0, 2, 4 it will make for example pair (0,4) and then it will check that value 2 is indeed between 0 and 4 so counter will increase). I tried the following code but it returned 0. How to fix it or is there an easier way to achieve that? I mainly care for the return value to be correct. thank you

import java.util.*;
import java.util.Map;
import java.lang.*;

public class Prac1 {
    public int count(int[] A){
        int k = 0;
        class PTemp{        
            int first = -1;
            int second = -1;
            public PTemp(int first, int second){
                int f = first;
                int s = second;             
            }           
        }
        List<PTemp> r = new ArrayList<PTemp>();
        for (int i = 0; i < A.length; i++) {
              for (int j = i+1; j < A.length; j++) {
                  r.add(new PTemp(A[i], A[j]));
                  r.add(new PTemp(A[j], A[i]));
                  //System.out.println("["+A[i] +","+A[j]+"]");
                  //System.out.println("["+A[j] +","+A[i]+"]");
              }
            }
        Iterator<PTemp> ir = r.iterator();
        while (ir.hasNext()){
            PTemp p = ir.next();
            for (int i = 0; i < A.length; i++){
                if (((p.first < A[i]) && (A[i] < p.second)) || ((p.first > A[i]) && (A[i] > p.second))){
                    k = k + 1;
                }
            }       
        }               
        return k;
    }
    public static void main(String[] args){
        int[] A = {0, 2, 4};
        Prac1 pr = new Prac1();
        System.out.println(pr.count(A));
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T06:55:22+00:00Added an answer on May 29, 2026 at 6:55 am

    Found a second bug in addition to the one mentioned by Alex D:

    class PTemp{        
            int first = -1;
            int second = -1;
            public PTemp(int first, int second){
                int f = first;
                int s = second;             
            }           
        }
    

    should really be:

    class PTemp{        
            int first = -1;
            int second = -1;
            public PTemp(int first, int second){
                this.first = first;
                this.second = second;             
            }           
        }
    

    Now the fields are properly set.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to write a method that when supplied an array of ints will
I have array of objects. I want to write method wich i will use
I want to write a method that will take an integer and return a
I want to write a method that removes all elements from a collection that
I want to re-write a method that has way too many nested if statements.
I want to write a C# method that can accept any number. Something like:
I want to write a method that simplifies the names of corporations. I would
I am creating a jasper report.In that I want to write one method which
I want to write a method that uses Reflection to tell whether a given
Hey guys. I have a method that gets called each second which I want

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.