5

I have a class called SparseMatrix. It contains an ArrayList of Nodes (also class). I am wondering of how to iterate through the Array and access a value in Node. I have tried the following:

 //Assume that the member variables in SparseMatrix and Node are fully defined.
 class SparseMatrix {
     ArrayList filled_data_ = new ArrayList();
     //Constructor, setter (both work)

     // The problem is that I seem to not be allowed to use the operator[] on
     // this type of array.
     int get (int row, int column) {
         for (int i = 0; i < filled_data_.size(); i++){
             if (row * max_row + column == filled_data[i].getLocation()) {
                 return filled_data[i].getSize();
             }
         }
         return defualt_value_;
     }
 }

I will probably switch to static arrays (and remake it every time I add an object). If anyone has a solution, I would very much appreciate you sharing it with me. Also, thank you in advance for helping me.

Feel free to ask questions if you don't understand anything here.

sudomeacat
  • 95
  • 1
  • 2
  • 11

5 Answers5

3

Assuming filled_data_ is a list that contains list of objects of a class named Node.

List<Nodes> filled_data_ = new ArrayList<>();
for (Node data : filled_data_) {
    data.getVariable1();
    data.getVariable2();
}

More info http://crunchify.com/how-to-iterate-through-java-list-4-way-to-iterate-through-loop/

HARDI
  • 394
  • 5
  • 12
  • 1
    I didn't mention this, but I am not going to parse the entire ArrayList. Still, this is good to know. Thank you. – sudomeacat Oct 08 '16 at 18:07
2

First of all, you should not use raw types. See this link for more info: What is a raw type and why shouldn't we use it?

The fix is to declare the type of object held by your array list. Change the declaration to:

 ArrayList<Node> filled_data_ = new ArrayList<>();

Then you can access each element in the array list using filled_data_.get(i) (as opposed to filled_data_[i], which would work for a regular array).

`filled_data_.get(i)`

The above will return the element at index i. Documentation here: https://docs.oracle.com/javase/7/docs/api/java/util/ArrayList.html#get(int)

Community
  • 1
  • 1
nhouser9
  • 6,730
  • 3
  • 21
  • 42
  • This helps a lot, thank you. (I have experience with C++, so Java concepts is rather new to me.) I found a way to do this with static arrays (inefficient), so I will take this into account. Again, thank you for your help. – sudomeacat Oct 08 '16 at 18:04
  • @diuqSehTnettiK no problem =] – nhouser9 Oct 08 '16 at 19:01
1

If you didn't use generic, then you need to cast the object

//Assume that the member variables in SparseMatrix and Node are fully defined.
 class SparseMatrix {
 ArrayList filled_data_ = new ArrayList();
 //Constructor, setter (both work)

 // The problem is that I seem to not be allowed to use the operator[] on
 // this type of array.
 int get (int row, int column) {
     for (int i = 0; i < filled_data_.size(); i++){
         Node node = (Node)filled_data.get(i);
         if (row * max_row + column == node.getLocation()) {
             return node.getSize();
         }
     }
     return defualt_value_;
 }

}

Faizal
  • 31
  • 5
0

If array list contains Nodes which defines getLocation() you could use :

((Nodes)filled_data_.get(i)).getLocation()

You could also define

ArrayList<Nodes> filled_data_ = new ArrayList<Nodes>();
c0der
  • 18,467
  • 6
  • 33
  • 65
0

When you create the ArrayList object, you should specify the type of the contained elements with <> brackets. It is also good to keep the reference to the List interface - not ArrayList class. To iterate through such a collection, use foreach loop:

Here is an example of the Node class:

public class Node {
    private int value;

    public Node(int value) {
        this.value = value;
    }

    public void setValue(int value) {
        this.value = value;
    }

    public int getValue() {
        return value;
    }
}

Here is an example of the Main class:

public class Main {

    public static void main(String[] args) {

        List<Node> filledData = new ArrayList<Node>();
        filledData.add(new Node(1));
        filledData.add(new Node(2));
        filledData.add(new Node(3));

        for (Node n : filledData) {
            System.out.println(n.getValue());
        }
    }
}
Wojtek
  • 1,288
  • 11
  • 16