بسم الله الرحمن الرحيم
المشكلة
كيف يمكن الحصول على عناصر List على هيئة مصفوفة نصية []String ؟
الحل
List l; // your linked list String[] items = (String[])l.toArray(new String[0]);
بسم الله الرحمن الرحيم
المشكلة
كيف يمكن الحصول على عناصر List على هيئة مصفوفة نصية []String ؟
الحل
List l; // your linked list String[] items = (String[])l.toArray(new String[0]);
ممتاز أول أنا أحسبه بس سي شارب اللي تسوي كاست على مصفوفة
لكن فيه شي ما فهمته ....
ليش بالضبط حاطين
new String[0]
بالنسبة للكاست بين المصفوفات فهذا الموضوع يطرحه أكثر
http://www.arabteam2000.com/vb/showthread....&threadid=14846
أما بالنسبة لأستخدام
new String[0]
بالفعل انا مستغرب من هذا الأجراء فلماذا لم تستعمل الصورة الأخرى لـ toArray() بدون معاملات خاصة اننا عملنا كاست للعناصر المرتجعة.
بالرجوع الى المساعدة الخاصة بصن ترتبك أكثر و تجدها هنا
http://java.sun.com/j2se/1.4.1/docs/api/ja...Collection.html
لذلك انا طرحت هذا الموضوع كسؤال فى منتديات صن و منتظر الأجابة.
خالد المصرى
اقتباسليش بالضبط حاطين new String[0]
It is used to specify the type of the destination array, in other words the array that will contain your data after converting the list to an array.It is used to cast the objects stored in the list one by one
C# uses the same logic :
ArrayList.ToArray(Type);
and both C# and Java have another toArray method that does not take an argument and return an array of objects.
Madani
will what I meant to say is that the first time i saw casting like ...
someArrayOfTypeA = ( ClassOfTypeA [] ) anotherArrayOfTypeB
was in C# However, I never expeareanced that with Java before :P
I think this type of casting is something like looping through the array and convert each and every element. In short, saving time.
I know about the ArrayList , which is equivelant to Vector in Java (or is it ?)
okay , new String[0] ?! , why the zero ?? and why the type ? you are converting explicitly to String[] using the (String []) , doesn't that new String[0] look a little bit acward to you ? it does to me !!
new string[0] is not used as a value but it is just used to get its type
The reason for doing that is that you can not cast from one array to another unless the source array has the same type as the destination
If you write
:
String[] items = (String[]) l.toArray();
you will get an error ,because the source array type is Object[] and not String[] though the data stored inside the source array is of type String,so the problem is with converting runtime types and not the data stored
To make sure that there is no problem with casting the data ,you could successfully run the code :
Object[] items = l.toArray();
for(int i=0;i<items.length;i++)
{
System.out.println((String) items);
}reading the toArray definition may help
public synchronized Object[] toArray(Object a[]) {
if (a.length < elementCount)
a = (Object[])java.lang.reflect.Array.newInstance(
a.getClass().getComponentType(), elementCount);
System.arraycopy(elementData, 0, a, 0, elementCount);
if (a.length > elementCount)
a[elementCount] = null;
return a;
}C# logic is similar to java ,since the type need to be retrieved using a value like
string[] m = new string[0]; ArrayList.ToArray(m.GetType());
بسم الله الرحمن الرحيم
وفوق كل ذى علم عليم
شكرا اخ مدنى على هذا النقاش المتعمق الذى لم اشهد مثله من زمن بعيد ولا تحرمنامن مثل هذه المواضيع القيمة
صح 2pac تراك من انصار السى شارب؟؟؟؟
خالد لمصرى
شكرا على التوضيح يا مدني ...
أنا من أنصار الجافا ... السي شارب هذي شغلتها ثانية ...
اخترت السي شارب لأني أحيانا أريد تطوير تطبيقات لتعمل فقط على الويندوز ... وطبعا السي شارب أسرع من جافا في الأداء
بسم الله الرحمن الرحيم
أسرع لأنها تخلت عن العديد من المميزات التى تقدمها الجافا و الأصل فى مقارنة البرمجيات الشبكية ليست من نقطة السرعة أكثر منه فى المميزات الأخرى كسهولة البرمجة و الأدوات المقدمة.
ايضا فى خطأ منتشر جدا بين أوساط المبرمجين هو مقارنة تطبيقات الجافا بتطبيقات الديسكتوب فتظهر الجافا كلغة بطيئة و هذا من أكثر الأسباب لنفور المبرمجين عنها.
خالد المصرى
هذا الموضوع مغلق.
المتواجدون خلال آخر دقيقتين · يتحدّث كل ٣٠ ثانية
جارٍ التحقق من المتواجدين…