hello,

What problems will I encounter if I use Array although I don' know how many elements need I store? If I set the array size very large than I expected, can problem arises anymore?


I tried to use Arraylist. But when there are many attributes, I always declare a class. I don't know how to use that many attributes and ArrayList.
I have got a headache.

Any suggesstions and answers are welcomed.

Well, for one, you will need to check the elements of the array to know when to stop processing, and where to add the next element. Also, I believe that as soon as an array is declared, the memory that will be needed is reserved immediately (at least that is how arrays work in other languages) so by declaring it artificially large you will be using much more memory than is needed, and this could wind up being a problem.

Moreover, I don't understand your statement about "many attributes" and ArrayLists. What exactly is it you are trying to do?

I THINK he's trying something like

class SomeClass {
    int[] data;
    
    int getField1() { return data[0]; }
    int getField2() { return data[1]; }
}

instead of the more reasonable

class SomeClass {
   int field1;
   int field2;

   int getField1() { return field1; }
   int getField2() { return field2; }
}

Still makes you wonder what the problems with ArrayList are for him.

Hello masijade&jwenting,

Thank you very much for your reply.
Here is my program.
I don't know how to access the attributes of a class.

class Crules{
         private int lp;
           private String tri;
            //private int count=0;
public Crules(int t1,String t2)
{
lp=t1;
tri=t2;
//count++;
}
public static void main(String args[])
{
        int t3=1;
       String t4="Hello";
      ArrayList theList=new ArrayList ();
     Crules crules=new Crules(t3,t4);
     theList.add(crules);
     System.out.print(theList.get(0).lp); //line 31
 }
}

The error message is
"Crules.java": Error #: 300 : variable lp not found in class java.lang.Object at line 31, column 38"

I am sorry if my question is a very stupid one.

System.out.print(((Crules)theList.get(0)).lp); //line 31

What is returned from the ArrayList (at least before Generics) is an object of class Object, so you need to cast this object to the correct class before you can access its fields and attributes.

Hello masijade,

Thank you very much for your help.
The program runs beautifully.

hello,

What problems will I encounter if I use Array although I don' know how many elements need I store? If I set the array size very large than I expected, can problem arises anymore?


I tried to use Arraylist. But when there are many attributes, I always declare a class. I don't know how to use that many attributes and ArrayList.
I have got a headache.

Any suggesstions and answers are welcomed.

HI ,
Idont know if i can help you in the correct mannerbut here what i would suggest is for array we have to initialise , and if you are assigning a high value , then one disadvantage could be wastage of memory, and if the size of the array is falling short at some time , then you will have array out of bounds exception,
and for array list , i feel it is best to use, we dont have to set the size , it is automatically increased with the number of variables at run time.
it should be not tough if you have many attributes and you are trying to use a class and you want to use a arraylist for it, i dont know in which context you want to use it and why u want to use it, if you can tell me then i can help you with that.

ArrayList al = new ArrayList(); OR new ArrayList(initialCapacity)
then something like
al.add(index, object) OR al.add(object)
then to retrieve your objects
al.get(index)
You can also convert the ArrayList to an Array so
al.toArray() which returns an array of type Object.
Hope this answers your question.

thanks for trying, but his question was already answered last week.

Bluebird, what masijade means is that you could also have declared the List as follows:

ArrayList<Crules> theList=new ArrayList <Crules>();

or even better (because as a general rule you should always define variables as the most general type applicable to the operations you want to perform on them):

List<Crules> theList = new ArrayList<Crules>();

Hi,
If you want to access the attributes of this class in another class, then they should not be private, declare them with public or with nothing .
Then in another class you will be able to acccess the variables of this class.

Bad advise, very bad.
You should learn about encapsulation before telling people to do things like that.
Not only are you dead wrong, what you suggest is the worst way to expose members to the outside world.

Hello,

I created arraylist and put the values in this way.
I don't know how to sort the arraylist according to one of the attributes, for instance the value of lp. I have seen many examples to sort the arraylist. But I don't understand.
Please help me.

class Crules{
         private int lp;
           private String tri;
            //private int count=0;
public Crules(int t1,String t2)
{
lp=t1;
tri=t2;
//count++;
}
public static void main(String args[])
{
        int t3=1;
       String t4="Hello";
      ArrayList theList=new ArrayList ();
     Crules crules=new Crules(t3,t4);
     theList.add(crules);
     System.out.print(((Crules)theList.get(0)).lp); 
 
       int t5=2;
       String t6="Hi";
      ArrayList theList=new ArrayList ();
     Crules crules=new Crules(t5,t6);
     theList.add(crules);
     System.out.print(((Crules)theList.get(1)).lp); 
 
}
}

hi ,

i would do the sorting in this way
i would create a class which implements Comparator
and implement a method
public int compare(Object o1, Object o2)
for example look at this


public class columncomparator implements Comparator {
public int compare(Object o1, Object o2) {
if (o1 != null && o2 != null) {
Crules col1 = new Crules ();
Crules col2 = new Crules ();
int lp1= col1.getlp();
int lp2= col2.getlp();
return (lp1- lp2);
}
return 0;
}
}

and i would suggest that you have a get and set methods for your local variable lp in the class Crules,

and then from the main method i will call the function columncomparator as follows
Collections.sort(theList, new columncomparator());

and this should solve the problem,
If it is not a good solution then definitely you can write it back to me, so that i can know at what place i am wrong

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