Question

Ok i need some help with a little syntax problem. I have a class and i want it to put it into an arraylist, and populate the arraylist using a for loop. Here is an example of what i want:

    public class w{
    int x;
    int y;
    }
    Arraylist m = new Arraylist();
    for(int i=0; i<n;i++)
  {
          m[i].add(w.x);
          m[i].add(w.y);

  }

Yes the code isn't functioning its just an example of what i want it to do. I don't know the syntax and I want an arraylist with classes that can be retrieved by giving the i and get the both variables only by that 'i'; Any help will be apreciated. Thank you very much for your time, sorry for the lousy description but i can't be more specific.

Was it helpful?

Solution

It's not clear what you're trying to accomplish, but perhaps this will be enough of a guide as to how to use ArrayList properly. I changed your class name from w to W to match the usual Java coding conventions.

public class W {
    int x;
    int y;
    public W(int x, int y) {
        this.x = x;
        this.y = y;
    }
}

ArrayList<W> m = new ArrayList<W>(); // can be 'new ArrayList<>()` in Java 7
m.add(new W(1, 2));
m.add(new W(5,-3));
// etc.

for (int i=0; i<m.size(); i++) {
    W w = m.get(i);
    System.out.println("m[" + i + "]=(" + w.x + "," + w.y + ")");
}

for (W w : m) {
    System.out.println("next W: (" + w.x + "," + w.y + ")");
}

OTHER TIPS

You cannot use the square bracket notation to access collections by index in Java. If and how you can depends on the particular collection API. In the case of List, there is a second version of the add method that takes an index as argument.

List<Integer> m = new ArrayList<>();
    for(int i=0; i<n;i++)
  {
          m.add(i, w.x);
          m.add(i, w.y);

  }

An ArrayList isn't an array in the traditional sense; it is an object. You will have to respect how one can access the elements of type List, which can be found with the Java 7 API

Namely you have the following two options available to you to place values into it.

.add(E element) which takes as a parameter an object that's of the same generic type, and

.addAll(Collection<? extends E> collection), which takes as a parameter another collection type. You can use this in conjunction with Arrays.asList(), if you're trying to place a primitive array into your List.

Some final things:

  • It's more desirable to code to the interface rather than the concrete type. This way, you don't care if you need to start using a LinkedList instead of an ArrayList.
  • You should type your list or Java will warn you of unchecked operations. These can be unsafe if you get carried away with what you place into your List.
  • Furthermore, you won't be able to retrieve the specific fields/functions from the List without using instanceof, since everything stored in there would be an Object, which isn't desirable.

You can use it this way..

public class w{
    int x;
    int y;
    }
  w ref1=new w();
  w ref2=new w();
    Arraylist<w> m = new Arraylist<w>();  
    m.add(ref1);
    m.add(ref2);

or you can do

for(some condition)
{
    m.add(new w());
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top