The pair class in the C++ standard library is used more. We can implement our own user-defined pair class in Java, and its object can be used anywhere like any other parameter.
Note : This class is equivalent to pair<int,int> class in java. You can create your own template or classes for other data types.
Syntax For defining the pair class can be:
class pair{ int first,second; // constructor for assigning values pair(int first,int second){ this.first = first; this.second = second; } // function which returns a pair pair values(){ return new pair(first,second); } // printing the pair class @Override public String toString(){ return "("+first+","+second+")"; } }
// Java program to create a pair class // and initialize an array of that // pair class object public class pair_example { public static void main(String[] args) { pair[] arr = new pair[5]; for (int i = 0; i < 5; i++) { arr[i] = new pair(i + 1, i + 2); } // printing an array of pairs easily for (pair i : arr) { System.out.println(i); } // to extract particular values int value = arr[3].second; System.out.println("Required Value = " + value); } } // user defined pair class of integer type class pair { int first, second; // constructor for assigning values pair(int first, int second) { this.first = first; this.second = second; } // function which returns a pair pair values() { return new pair(first, second); } // printing the pair class @Override public String toString() { return first + "," + second; } }
Output
1,2 2,3 3,4 4,5 5,6 Required Value = 5
Implementation Example:
Suppose we'd like to store the ith prime and its index together during a structure.
// Java program to create a struct/pair class // for storing a prime number with its index class GFG { public static void main(String[] args) { // first N prime numbers int N = 30; // creating list of pairs java.util.ArrayList p = new java.util.ArrayList<>(); int index = 1; for (int i = 1; i <= N; i++) { if (isPrime(i)) { // creating new pair object and appending to // list p.add(new pair(index++, i)); } } System.out.println("i and the ith prime numbers are :"); System.out.println(p); } // function to check prime static boolean isPrime(int n) { if (n < 2) return false; for (int i = 2; i * i <= n; i++) { if (n % i == 0) return false; } return true; } } // user defined pair class of integer type class pair { int first, second; // constructor for assigning values pair(int first, int second) { this.first = first; this.second = second; } // function which returns a pair pair values() { return new pair(first, second); } // printing the pair class @Override public String toString() { return "(" + first + "," + second + ")"; } }
Output
i and the ith prime numbers are : [(1,2), (2,3), (3,5), (4,7), (5,11), (6,13), (7,17), (8,19), (9,23), (10,29)]
Australia
UK
UAE
Singapore
Canada
New
Zealand
Malaysia
USA
India
South
Africa
Ireland
Saudi
Arab
Qatar
Kuwait
Hongkong
Copyright 2016-2023 www.programmingshark.com - All Rights Reserved.
Disclaimer : Any type of help and guidance service given by us is just for reference purpose. We never ask any of our clients to submit our solution guide as it is, anywhere.