How to store an array of pairs in Java?

前端 未结 9 1328
日久生厌
日久生厌 2020-12-15 06:51

I am new to Java, I want to store an array of pair of doubles. My code looks like this:

import java.util.ArrayList;
import java.util.Map.Entry;

List

        
相关标签:
9条回答
  • 2020-12-15 07:19

    For someone into competitive programming or coding interview questions you may have a requirement to write all the code in a single file/editor window

    in that case, you may use inner classes to conveniently make a pair and then use an object of that class as a pair

    if you want to create a stack of Pairs you can create a Pair object and put it in the stack

    import java.util.*;
    public class PairArray
    {
        static class Pair{
            int a,b;
            Pair(int a,int b){
                this.a=a;
                this.b=b;
            }
        }
        static void main()
        {
            int a,b,l=10;
            Scanner sc = new Scanner(System.in);
            Pair array[]= new Pair[l];
            for(int i=0;i<l;i++){
               a=sc.nextInt();
               b=sc.nextInt();
               array[i]=new Pair(a,b);
            }
            for(int i=0;i<l;i++){
               System.out.println(array[i].a+" "+array[i].b);
            }
    
        }
    }
    
    0 讨论(0)
  • 2020-12-15 07:23

    Couldn't you just use

    public class MyClass<A,B> extends ArrayList{
    private A first;
    private B second;
    public MyClass(A first, B second){
    super();
    this.first = first;
    this.second = second;}
    }
    

    and then add some form of add method, along with a first and second accessor & mutator method? I'm sort of new to programming, but this way would seem like it might work, and be accessible to things other than just the DOUBLE, (in case down the road you want to use other types, like Integer, or even String).

    0 讨论(0)
  • 2020-12-15 07:26

    Another approach, and probably the most efficient way to store and array of double pairs is to use a single array of doubles, and use (2*i) and (2*i + 1) as your indexing scheme. Additionally you gain the advantage that the array will be initialized to all 0s when you create it, no additional steps required. Unfortunately there is a little extra coding overhead to implement add() and remove(), but surprisingly, it's probably less than creating your own container class for the pair.

    class MyClass {
        double[] values;
        int count;
    
        MyClass(int initialCapacity) {
            values = new double[initialCapacity*2];
        }
    
        // adding a pair
        void addPair(double x, double y) {
            if (count*2 >= values.length) {
                values = Arrays.copyOf(values, values.length*2);
            }
            values[count*2] = x;
            values[count*2 + 1] = y;
            count++;
        }
    
        void remove(int index) {
            if (index >= count) throw new IndexOutOfBoundsException();
    
            if (index < --count) {
                System.arraycopy(values, (index+1)*2, values, index*2, (count - index) * 2);
            }
        }
    
        int size() { return count; }
    
        // both these should check that index < count.
        double getX(int index) { return values[index*2]; }
        double getY(int index) { return values[index*2 + 1]; }
    
        void exampleIteration() {
            // getX/Y accessors are examples of how to get
            // the values, but it will be more efficient
            // in most cases to just access the array
            // array directly as so...
            for (int i=0 ; i<count ; ++i) {
                System.out.printf("%d: (%f,%f)%n", i, values[i*2], values[i*2+1]);
            }
        }
    }
    
    0 讨论(0)
  • 2020-12-15 07:27

    The Map.Entry type that you are trying to use is just an interface, and can therefore not be instantiated. If you wanted to (mis)use internal types of Map, then the concrete Map.Entry implementation HashEntry would be an option.

    It is however a much better idea to implement you own Pair type. Or to use a Map instead of an array if that suits you needs.

    0 讨论(0)
  • 2020-12-15 07:35

    Create your own class to represent a pair and add a constructor that takes two arguments:

    public class MyPair
    {
        private final Double key;
        private final Double value;
    
        public MyPair(Double aKey, Double aValue)
        {
            key   = aKey;
            value = aValue;
        }
    
        public Double key()   { return key; }
        public Double value() { return value; }
    }
    

    See this answer for reasons why a Pair does not exist in Java: What is the equivalent of the C++ Pair<L,R> in Java?

    0 讨论(0)
  • 2020-12-15 07:35

    You could use a map to solve this.

    0 讨论(0)
提交回复
热议问题