بروز خطا
[message]
اشتراک در سوال
رای ها
[dataList]
استخراج یک عنصر مشخص از آرایه
سلام دوستان،فرض کنید یه آرایه از مثلا تعدادی دانش آموز داریم؛که هر کدوم دارای یک سری ویژگی هستند،فرض کنیم شماره دانش آموزشی یکتایی دارند!حالا من میخام با استفاده از دستور get اون دانش آموزی رو از آرایم انتخاب کنم که برفرض شماره دانش آموزیش 12 هست،چطوری ایندکس اون دانش آموز مشخص رو بدست بیاریم؟
0
0
یه چیزایی توی زبان های دیگه مثل #F و #c هست که اگه اشتباه نگنم بهش میگن دیکشنری! مثلا اسم شهرها و کد شهرها رو میدی به اون. بعد میتونی اسم یک شهر رو وارد کنی و بعد کدش رو تحویل بگیری. یه سرچ بکن شاید مشابهش توی جاوا هم باشه. ما هم دعات میکنیم (12 سال پیش)
برای این سوال 2 پاسخ وجود دارد.
پاسخ به سوال
مقدسیان
12 سال پیش
0
0
کد نمونه کار با دیکشنری در جاوا
مرجع: runnable->Java Dic
/*
Dictionary implements a key value pair kind of collection
where the key is unique
*/
import java.util.*;
public class DictionaryDemo {
static String newLine = System.getProperty("line.separator");
public static void main(String[] args) {
System.out.println(newLine + "Dictionary in Java" + newLine);
System.out.println("-----------------------" + newLine);
System.out.println("Adding items to the Dictionary" + newLine);
//Creating dictionary object
//dictionary can be created using HashTable object
//as dictionary is an abstract class
Dictionary dict = new Hashtable();
//you add elements to dictionary using put method
//put(key, value)
dict.put(1, "Java");
dict.put(2, ".NET");
dict.put(3, "Javascript");
dict.put(4, "HTML");
System.out.println(newLine + "Items in the dictionary..." + dict + newLine);
System.out.println("-----------------------" + newLine);
//elements can be retrieved using their key
System.out.println("Retrieve element from dictionary with key 1 : " +
dict.get(1) + newLine);
System.out.println("-----------------------" + newLine);
//elements can be removed using their key
System.out.println("Removing element from dictionary with key 2 : " +
dict.remove(2) + newLine);
System.out.println("Items in the dictionary after removing..." + dict + newLine);
System.out.println("-----------------------" + newLine);
}
}
خیلی بیشتر از اون چیزی که انتظار داشتی رو میتونی با دیکشنری اتجام بدی
پاسخ به سوال
uncocoder
12 سال پیش
+1
0
غیر از کدهای دوستان، چنانچه فقط بحث در دو مقدار است، مثل ID دانش آموز و خصوصیات آن ( که یک کلاس است ) می تونید از HashMap استفاده کنید. مثل زیر ( کدهای زیر رو هوا نوشته شده، تست کنید ):
HashMap<Integer, Student> students = new HashMap<Integer, Student>();
Student gholi = Student("Gholi", "Male", 23)
students.put(1, gholi);
students.put(2, new Student("Ali", "Male", 24));
students.put(3, new Student("Sara", "Female", 23));
students.getValue(2);
students.getKey(gholi);
پاسخگویی و مشاهده پاسخ های این سوال تنها برای اعضای ویژه سایت امکان پذیر است .
چنانچه تمایل دارید به همه بخش ها دسترسی داشته باشید میتوانید از این بخش لایسنس این آموزش را خریداری نمایید .