الفريق العربي للبرمجةأرشيف المنتديات · 2000 – 2023
نسخة أرشيفية للقراءة فقط — التسجيل والمشاركة مغلقان، والمحتوى محفوظ كما كان.

Collections

مغلق
بدأه المبرمج1 في 5 يوليو 2004 · 8 رد · 2,628 مشاهدة · في JavaSE
مشاركة: واتساب X فيسبوك تيليجرام
#1 صاحب الموضوع

we know that String Class is Fix class , that is complex to edit, add to it and remove from it , and if you need to do advence process like sort or search , then it will take good number of code.

but in jave there is class called Collections it can reduce that effort ..

here is some simple examples of how to use that class to manipulate String easily

import java.util.*; // <- this must be included

  public class TheMain {

  public static void main(String[] args) {

    ///
    System.out.println("\n");


    // names
    String Names[] =
    {"Programmer1","Hamad","Samer","Rami","Fahd","Abdullah","Saad","Omar" };

    List list; // <- this is our new class


    // print the String
    System.out.print("The String Names: ");
    for(int i = 0; i < Names.length; i ++)
    {System.out.print(" "+Names);}

    // copy String to list
    list = new ArrayList(Arrays.asList(Names));

    // print the List
    System.out.println("The List list: "+ list);

    //sort the list (Increment sorting)
    Collections.sort(list);
    System.out.println("The List After sort: "+list);

    // rearrange the list randomly
    Collections.shuffle(list);
    System.out.println("The List After shuffle: "+list);

   //find the max and min
   System.out.println("\tThe Max Name is: "+Collections.max(list));
   //find the Min and min
   System.out.println("\tThe Min Name is: "+Collections.min(list));

   // search
   System.out.println("The Index of \"Samer\" in list is: "+Collections.binarySearch(list,"Samer"));
   System.out.println("The Index of \"Programmer1\" in list is: "+Collections.binarySearch(list,"Programmer1"));
   System.out.print("The Index of \"Ahmed\" in list is: "+Collections.binarySearch(list,"Ahmed"));
   System.out.println("  if index = -ve 'then not in list' ");

   //insert
   list.add(2,"Ahmed");
   System.out.println("The List After adding 'Ahmed' in the 2nd index: "+list);

   // remove
   list.remove(4);
   System.out.println("The List After removing 4th index: "+list);

   // return back to String
   Names = (String[]) list.toArray(new String[list.size()]);
   System.out.print("The String Names: ");
   for(int i = 0; i < Names.length; i ++)
   {System.out.print(" "+Names);}

    ///


  }//end the main method


}// end TheMain class

and if any question... Just Aske

تم تعديل هذه المشاركة بواسطة المبرمج1 في 5 يوليو 2004 في 14:31

#2

مشكورالمبرمج 1

يا اخي لو سمحت اكتب بالعربي احنا عرب ولغتنا العربية لغة القران الكريم واحنا بمنتدى الفريق العربي (مو الانجليزي) للبرمجة

والسلام

لست متواجد بشكل يومي

#3

thanks Student_1981

I am An Arabic person ... and Love the Arabic Language

and I like to write in Arabic ... but My PC don't support Arabic Language ..

#4

المبرمج1 شكرا لذكرك اسمي في الكود هه ه ههه هه

و انا لا اشك في لحظة في عروبة المبرمج1 ...

و التمس لاخيك المسلم الف عذر قبل التهجم عليه

Theory is when you know something, but it doesn't work. Practice is when something works, but you don't know why. Programmers combine theory and practice: Nothing works and they don't know why

#5

اخي رامي انا ما تهجمت عليه كانت بس ملاحظة والله انا الكمبيوتر مابعرف اكتب في عربي عشان هيك عندما اريد ان اكتب رد اذهب الى النت على لوحة مفاتيح عربية واكتب وبعدها copy و paste ومتاسف اخي المبرمج 1 ما قصدت اتهجم عليك

لست متواجد بشكل يومي

#6

thank you رامي العيساوي

and thank you Student_1981

:)

تم تعديل هذه المشاركة بواسطة المبرمج1 في 7 يوليو 2004 في 07:46

#7

حصل خير...

Theory is when you know something, but it doesn't work. Practice is when something works, but you don't know why. Programmers combine theory and practice: Nothing works and they don't know why

#8

أعظم الشكر لك أخي المبرمج1 فقد أفدتني في هذا الكود كثيراً و نتمنى المزيد مع الشرح

بدي أسألك هل List هنا هي interface :

List list; // <- this is our new class

و هل من الضروري أن يكون الصف الذي يحقق الواجهة أي

class name implements interface

أن يكون abstract أي :

abstract class name implements interface

تم تعديل هذه المشاركة بواسطة hani1 في 7 يوليو 2004 في 18:13

الحياة كلها وقفة عز فقط

------------------------------------------

المجتمع معرفة و المعرفة قوة

#9

hani1 ...

i don't think that "List " is an interface ...

you can use it by its own methods ...

or by using Collections method ...

...

i hope this answer the question :)

هذا الموضوع مغلق.

مواضيع مشابهة