2

I need to create dynamically changing array of object. how to create this. pls help me.

PrefixMatcher[] pm = new PrefixMatcher[8];
Scanner infile = new Scanner(new File(url.toURI()));
while (infile.hasNextLine()) {
      int len = infline.length();
      //here i need to create reinitialize the same object using length of the file's first line      

}
5
  • 1
    Any reason you cannot use an ArrayList? Commented Feb 13, 2014 at 6:18
  • 1
    i have to get the details of particular object using object only i.e pm[124] like this. in pm[124] object i stored unique value. but in ArrayList we need to search every item. Commented Feb 13, 2014 at 6:21
  • pm.get(124); would do it if pm was an ArrayList. More: docs.oracle.com/javase/7/docs/api/java/util/… Commented Feb 13, 2014 at 6:22
  • @user3300015: What??? Commented Feb 13, 2014 at 6:22
  • @user3300015 you can access the value stored in arraylist at particular location by using get. Please see my answer. Commented Feb 13, 2014 at 6:23

4 Answers 4

1

Arrays in Java are of fixed size that is specified when they are declared. To increase the size of the array you have to create a new array with a larger size and copy all of the old values into the new array.

ex:

char[] copyFrom  = { 'a', 'b', 'c', 'd', 'e' };
char[] copyTo    = new char[7];

System.out.println(Arrays.toString(copyFrom));
System.arraycopy(copyFrom, 0, copyTo, 0, copyFrom.length);
System.out.println(Arrays.toString(copyTo));

another example:

 String[] array = new String[5];
 ...
 array = expand(array, 10);

 private String[] expand(String[] array, int size) {
   String[] temp = new String[size];
   System.arraycopy(array, 0, temp, 0, array.length);
   for(int j = array.length; j < size; j++)
      temp[j] = "";
   return temp;
}
Sign up to request clarification or add additional context in comments.

1 Comment

I'm going to give you a +1 for making the attempt to at least answer the OP's question as it's asked...I'd like a mention of List/Collections, but seen as we can't clarify why the OP can/can't use these, this is the closes to actually giving them an answer...
0

ArrayList is what you are looking for, an implementation of List which uses array internally to store data

Comments

0

Try using ArrayList. ArrayList can be used when size of array is not fixed.

ArrayList<PrefixMatcher> pm = new ArrayList<PrefixMatcher>();

You can access the elements at particular location by giving index.

pm.get(123);

Comments

0

You can't resize an array, I think that much is obvious by now, but, if you can't use of the of List implementations of the Collections API, you can create a new array, that is larger than the first, and copy it's contents to it, then replace the reference.

String[] values = new String[10];
String[] newValues = new String[20];
System.arraycopy(values, 0, newValues, 0, values.length);
values = newValues;

You could wrap this in a method to make it easier...

public String[] grow(String[] source, int newSize) {
    String[] newValues = new String[20];
    System.arraycopy(source, 0, newValues, 0, source.length);
    return newValues;
}

Personally, though...I'd seriously consider using something like ArrayList or LinkedList...it's just simpler ;)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.