Hi guys,

For multi dimentional arrays, i know the amount of columns- 3. But I do not know the amount of rows because each input file has a diff. no. of rows(which can go up to a few thousands). So how do I make an array out of it? Thank you all in advance!

You don't use arrays, you use a Vector
Search for tutorials about Vector and/ or lists in general (ArrayList)

With Vectors you can add as many elements as you want it doesn't have a fixed size. Whenever you add more its size increases automatically, so you can add more.
You can add a single dimensional array as an element of a Vector or better , write a class that represents your input. When you read each line of the file create an instance, set its values and add it to the Vector.

You don't use arrays, you use a Vector
Search for tutorials about Vector and/ or lists in general (ArrayList)

With Vectors you can add as many elements as you want it doesn't have a fixed size. Whenever you add more its size increases automatically, so you can add more.
You can add a single dimensional array as an element of a Vector or better , write a class that represents your input. When you read each line of the file create an instance, set its values and add it to the Vector.

Thank you for your reply. But I need to translate using a matrix later, so is it possible to do that? vector * array??

is it possible to do that? vector * array??

You'll have to write the code to do that. Unless you can find a third party package that does it.

You'll have to write the code to do that. Unless you can find a third party package that does it.

I see. Thank you. So it's possible? It will be compatible? I can actualy write a JAVA code to do so?

If you need a multidimensional array then instead of an object, you can add arrays to the Vector.

Usually we read a file that has lines like this:
Name Age Phone
Jack 20 1122333

Into an object:

class Person {
String name;
int age;
long phone;
}

And since the file has many lines we use a Vector:

Vector v = new Vector();

while (read file) {
Person p = new Person();

v.add(p);
}

But in your case you can do this:

Vector<int []> v = new Vector<int []>();
      
int [] array1 = new int[5];  
array1[0] = ......
v.add(array1);

int [] array2 = new int[5];  
array2[0] = ......
v.add(array2);

int [] tt = v.get(0);

When you do this: Vector<int []> you explicitly say that that vector will only accept arrays of integers. You can omit it, but with that way the compiler would allow you to add any type of elements which is not very safe.

Vector v = new Vector();
v.add("aaaa");
v.add(1);
v.add(new SomeObject());

The above will compile. But then you would have to do casting in order to get the right type of element because at the above case the v.get(i) method returns an Object. With the first example the v.get(i) returns an int [] so no casting is required.

Also this <int []> part of code works only for java 1.5 and higher.

So you can add as many arrays you want.
And if you want the element at [2][3] of your "multidimensional" array, then: v.get(2)[3] v is a Vector
v.get(2) is an array
v.get(2)[3] 4th element
v.get(2).length the length of the array at position 2 of the Vector

for (int i = 0;i<v.size();i++) {
  int [] array = v.get(i);
  // loop the array as usual 
}

If you need a multidimensional array then instead of an object, you can add arrays to the Vector.

Usually we read a file that has lines like this:
Name Age Phone
Jack 20 1122333

Into an object:

class Person {
String name;
int age;
long phone;
}

And since the file has many lines we use a Vector:

Vector v = new Vector();

while (read file) {
Person p = new Person();

v.add(p);
}

But in your case you can do this:

Vector<int []> v = new Vector<int []>();
      
int [] array1 = new int[5];  
array1[0] = ......
v.add(array1);

int [] array2 = new int[5];  
array2[0] = ......
v.add(array2);

int [] tt = v.get(0);

When you do this: Vector<int []> you explicitly say that that vector will only accept arrays of integers. You can omit it, but with that way the compiler would allow you to add any type of elements which is not very safe.

Vector v = new Vector();
v.add("aaaa");
v.add(1);
v.add(new SomeObject());

The above will compile. But then you would have to do casting in order to get the right type of element because at the above case the v.get(i) method returns an Object. With the first example the v.get(i) returns an int [] so no casting is required.

Also this <int []> part of code works only for java 1.5 and higher.

So you can add as many arrays you want.
And if you want the element at [2][3] of your "multidimensional" array, then: v.get(2)[3] v is a Vector
v.get(2) is an array
v.get(2)[3] 4th element
v.get(2).length the length of the array at position 2 of the Vector

for (int i = 0;i<v.size();i++) {
  int [] array = v.get(i);
  // loop the array as usual 
}

Thank you! But I don't really understand as this is the first time I'm looking through this whole new vector thing.

This article has been dead for over six months. Start a new discussion instead.