1

how can I make array of ArrayList or Pair Class which I made myself at the code below.

ex1)

import java.util.*;

class Pair{
  static int first;
  static int second;
}

public class Main{
  public static void main(String[] args){
    Vector<Pair>[] v = new Vector<Pair>[100](); //this gives me an error
  }
}

1.why the code above gives me an error? 2.my goal is to make an array of vector so that each index of vector holds one or more Pair classes. How can I make it?

another example) : array of ArrayList

import java.util.*;

public class Main{
  public static void main(String[] args){
    ArrayList<Integer> arr = ArrayList<Integer>(); //I know this line doesn't give error
    ArrayList<Integer>[] arr = ArrayList<integer>[500]; // this gives me an error
  }
}

3.why does the code above give me an error? 4.my goal is to make an array of ArrayList so that each index of Array has ArrayList/Queue/Vector/Deque whatever. How can I make it?

hongtaesuk
  • 557
  • 5
  • 10
  • 19
  • Possible duplication of http://stackoverflow.com/questions/5662394/java-1-6-creating-an-array-of-listt – Sanghyun Lee Jul 31 '11 at 13:59
  • 1
    This isn't worth being an answer, but none of the answers mention it. Making your members `static` means each and every instance of Pair will always have exactly the same values... probably not what you want. Just remove the `static` and you'll be much happier. – CPerkins Jul 31 '11 at 14:44

4 Answers4

3

How about a full generic solution:

ArrayList<ArrayList<Integer>> arr = new ArrayList<ArrayList<Integer>>();
MByD
  • 135,866
  • 28
  • 264
  • 277
2

The syntax you have used is not what Java uses. If you want to have an array of ArrayLists then do:

ArrayList[] arr = new ArrayList[100];

for(int i=0; i<arr.length; i++)
{
    arr[i] = new ArrayList<Pair>(); // add ArrayLists to array
}

Here the type argument <Pair> specifies that the ArrayList should contain items of type Pair. But you can specify any type you wish to use. The same goes for ArrayList, you could replace ArrayList with Vector in the example.

It would be best to use an ArrayList instead of an array in the example. Its much easier to maintain without worrying about the changing length and indexes.

Hope this helps.

adamjmarkham
  • 2,150
  • 2
  • 18
  • 26
  • one more question, if I do "arr[i].add(p)"; p.first=1, p.second=2; – hongtaesuk Jul 31 '11 at 14:17
  • how can I refer to p.first of arr[i]? – hongtaesuk Jul 31 '11 at 14:18
  • @AdamJMTech - I have a little problem with your answer. You lose all the advantage of generics when you create an array of `ArrayList`, since no type checking / casting will be done for you, you could as well use just `ArrayList` and not `ArrayList`, since `arr[i]` is not bounded to any class. – MByD Jul 31 '11 at 14:29
  • @hongtaesuk Yes `"arr[i].add(p)"; p.first=1, p.second=2;` should work. – adamjmarkham Jul 31 '11 at 14:59
  • @MByD I agree which is why I put at the end of my post that using an ArrayList instead of an array would be preferred to make it type-safe. But the original question was how to use an array to do it. – adamjmarkham Jul 31 '11 at 15:00
  • when I print arr[0].get(0) (when previously added Pair type p in the arr[0]) and p it will print exactly the same address but when I do arr[0].get(0).first, it gives me an error but p.first doesnt. what can I do so as not to get an error from arr[0].get(0).first – hongtaesuk Aug 01 '11 at 03:50
  • @hongtaesuk I assume you have sorted out this problem. Let me know if you are still getting this error. – adamjmarkham Aug 05 '11 at 13:24
1
public static void main(String[] args){
    Vector[] v = new Vector[5];
        for(int i=0;i<5;++i){
            v[i]= new Vector<Pair>();
    }
  }
RiaD
  • 46,822
  • 11
  • 79
  • 123
1

I don't know java that well, but don't you want to do: ArrayList<ArrayList<Pair>> v = new ArrayList<ArrayList<Pair>>();

Try to break down what containers you need in your question. Your goal is to make a ArrayList (ok, the outer ArrayList satisfies that purpose) that has one or more pair classes in that. "That has" means that "each item in the ArrayList is this type". What would we use to store one or more Pair classes? Another Vector/List of tyoe Pair. So each item in the outer ArrayList is another ArrayList of Pairs.

Note: I moved everything to ArrayList because I read that Vector is somewhat deprecated and they serve similar functions. You may want to check on this.

This example should help with with the next part of your question, but let me know if it doesn't,

skaz
  • 21,962
  • 20
  • 69
  • 98