Jump to content

how to put objects as an array element java

- - - - -

  • Please log in to reply
14 replies to this topic

#1
Animus

Animus

    Newbie

  • Members
  • PipPip
  • 26 posts
how do you do it?

im making an arraylist of objects
and i need a searching algorithm to search that list and print out the each object within the element.. :(

public static void main(String[] args)throws IOException {

        BufferedReader b =new BufferedReader(new InputStreamReader(System.in));

        Student s1=new Student(new StudentName("Marjone", "P","Yecla"),19,new Course("BSIT"),new StudentChairLocation(0,1));

        Student s2=new Student(new StudentName("Micheal", "j","Celeste"),17,new Course("BSCS"),new StudentChairLocation(0,2));

        

        

        ArrayList<Student> aListStudents = new ArrayList<Student>();


        aListStudents.add(s1);

        aListStudents.add(s2);

       


        

        


        System.out.println(aListStudents);


#2
anjelo

anjelo

    Newbie

  • Members
  • PipPip
  • 21 posts
i am newbie,more to learn...

#3
gregwarner

gregwarner

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 853 posts
  • Location:Arkansas
You need a loop. (I assume all you want to do is print each element in the list sequentially, correct?)
Hofstadter's Law: It always takes longer than you expect, even when you take into account Hofstadter's Law.

– Douglas Hofstadter, Gödel, Escher, Bach: An Eternal Golden Braid


#4
fread

fread

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 787 posts

Animus said:

how do you do it?

im making an arraylist of objects
and i need a searching algorithm to search that list and print out the each object within the element.. :(

public static void main(String[] args)throws IOException {
        BufferedReader b =new BufferedReader(new InputStreamReader(System.in));
        Student s1=new Student(new StudentName("Marjone", "P","Yecla"),19,new Course("BSIT"),new StudentChairLocation(0,1));
        Student s2=new Student(new StudentName("Micheal", "j","Celeste"),17,new Course("BSCS"),new StudentChairLocation(0,2));
        
        
        ArrayList<Student> aListStudents = new ArrayList<Student>();

        aListStudents.add(s1);
        aListStudents.add(s2);
       

        
        

        System.out.println(aListStudents);

If you are going to work on each item of the array you don't need a search. You just need to iterate the list.
for(i = 0; i < some_amount; i++)
{
       Student stu =  aListStutend.get(i);
       //do other things
}


If you want to do some type checking first:

for(i = 0; i < some_amount; i++)
{
      Object a =  aListStudents.get(i);
      if(a instanceof Student)
      {
          Student b = (Student)a;
      }
     else {
            //do something else
           }
}

Perfection of means and confusion of ends seem to characterize our age. Albert Einstein :confused:

#5
Animus

Animus

    Newbie

  • Members
  • PipPip
  • 26 posts
yeah. but it also involes a searching cause the user would input a name then it searches the list and prints out all the objects sequentially. :(

---------- Post added at 03:28 PM ---------- Previous post was at 03:12 PM ----------

fread said:

If you are going to work on each item of the array you don't need a search. You just need to iterate the list.

for(i = 0; i < some_amount; i++)

{

       Student stu =  aListStutend.get(i);

       //do other things

}



If you want to do some type checking first:


for(i = 0; i < some_amount; i++)

{

      Object a =  aListStudents.get(i);

      if(a instanceof Student)

      {

          Student b = (Student)a;

      }

     else {

            //do something else

           }

}



This one really helps! my problem now is how do i get the users input and find a match in a list... >_<

#6
fread

fread

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 787 posts
Ok. I removed my post from before. I just read the last line of your post. Where are you stuck?
Perfection of means and confusion of ends seem to characterize our age. Albert Einstein :confused:

#7
Animus

Animus

    Newbie

  • Members
  • PipPip
  • 26 posts
actually thanks to your help i got the idea how i will be printing it the problem now is
the part where the user inputs the name and searches the list for a match. :(

---------- Post added at 03:39 PM ---------- Previous post was at 03:36 PM ----------

oh.. and the input has to be string it would look like this

//enter name:

//Search object in list
//Finds object
//Prints out all the objects in the element where it was found

#8
fread

fread

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 787 posts
You seem to be on the right path.
Perfection of means and confusion of ends seem to characterize our age. Albert Einstein :confused:

#9
extremejava

extremejava

    Newbie

  • Members
  • Pip
  • 2 posts
You can use Arrays class.

#10
fread

fread

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 787 posts

extremejava said:

You can use Arrays class.
To do what exactly?
Perfection of means and confusion of ends seem to characterize our age. Albert Einstein :confused:

#11
Animus

Animus

    Newbie

  • Members
  • PipPip
  • 26 posts

fread said:

You seem to be on the right path.

yeah. most of the code is done. but there is still some stepping stones missing here, i wished i reviewed more, i really dont know how to search an array list. now do i have an idea whats the IO for this thing, :(

#12
fread

fread

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 787 posts
If you are not doing anything do heavy a sequential search should be sufficient for now. For each object instance you collect, just compare the attributes with the search critiria. For example, lets say you are looking for a student with name Animus J Fread
for(i = 0; i < some_amount; i++)
{
     Student stu = aListStudents.get(i);
     String fname =  stu.getFirstName();
     Char midchar = stu.getMidChar();
     String lname = stu.getLastName();
     //now that you have each persons name you can do comparisons for a match,
     //be it first name alone, first and last or all three fields.
     //you can extract the other attributes in a similar manner and do comparisons.
     //do whatever based on your findings; continuing searching, stop, process, etc.
}
I recommed you have a look at the String Class; very useful stuff for string manipulations and comparisons.
Perfection of means and confusion of ends seem to characterize our age. Albert Einstein :confused:




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users