importjava.util.ArrayList;importjava.util.List;importjava.util.Random;publicclassProductInventory{privatestaticfinalList<Product>products=newArrayList<>();privatestaticfinalRandomrandom=newRandom();publicstaticvoidmain(String[]args){addProduct("Product1");addProduct("Product2");addProduct("Product3");System.out.println("All Products:");for(Productproduct:products){System.out.println("Name: "+product.getName()+", ID: "+product.getId());}}publicstaticvoidaddProduct(StringproductName){StringrandomId;booleanidIsUnique;longstartTime;longendTime;longduration;// Record start timestartTime=System.nanoTime();do{randomId=getRandomId();idIsUnique=true;for(Productproduct:products){if(product.getId().equals(randomId)){idIsUnique=false;System.out.println("ID "+randomId+" already exists. Generating a new ID.");break;}}}while(!idIsUnique);products.add(newProduct(productName,randomId));System.out.println("Added "+productName+" with ID: "+randomId);// Record end time and calculate durationendTime=System.nanoTime();duration=endTime-startTime;System.out.println("Time taken to add "+productName+": "+duration+" nanoseconds");}publicstaticStringgetRandomId(){return"ID"+random.nextInt(1000);}}// Product class to store product name and IDclassProduct{privatefinalStringname;privatefinalStringid;publicProduct(Stringname,Stringid){this.name=name;this.id=id;}publicStringgetName(){returnname;}publicStringgetId(){returnid;}}
レスポンス
セットを使用して商品を保存する
importjava.util.HashSet;importjava.util.Random;importjava.util.Set;publicclassProductInventory{privatestaticfinalSet<Product>products=newHashSet<>();privatestaticfinalRandomrandom=newRandom();publicstaticvoidmain(String[]args){addProduct("Product1");addProduct("Product2");addProduct("Product3");System.out.println("All Products:");for(Productproduct:products){System.out.println("Name: "+product.getName()+", ID: "+product.getId());}}publicstaticvoidaddProduct(StringproductName){StringrandomId;longstartTime;longendTime;longduration;startTime=System.nanoTime();do{randomId=getRandomId();}while(!products.add(newProduct(productName,randomId)));System.out.println("Added "+productName+" with ID: "+randomId);endTime=System.nanoTime();duration=endTime-startTime;System.out.println("Time taken to add "+productName+": "+duration+" nanoseconds");}publicstaticStringgetRandomId(){return"ID"+random.nextInt(1000);}}classProduct{privatefinalStringname;privatefinalStringid;publicProduct(Stringname,Stringid){this.name=name;this.id=id;}publicStringgetName(){returnname;}publicStringgetId(){returnid;}@Overridepublicbooleanequals(Objectobj){if(this==obj)returntrue;if(obj==null||getClass()!=obj.getClass())returnfalse;Productproduct=(Product)obj;returnid.equals(product.id);}@OverridepublicinthashCode(){returnid.hashCode();}}
[[["わかりやすい","easyToUnderstand","thumb-up"],["問題の解決に役立った","solvedMyProblem","thumb-up"],["その他","otherUp","thumb-up"]],[["Hard to understand","hardToUnderstand","thumb-down"],["Incorrect information or sample code","incorrectInformationOrSampleCode","thumb-down"],["Missing the information/samples I need","missingTheInformationSamplesINeed","thumb-down"],["翻訳に関する問題","translationIssue","thumb-down"],["その他","otherDown","thumb-down"]],["最終更新日 2024-12-05 UTC。"],[],[]]