Jagged Array in Java - GeeksforGeeks

文章推薦指數: 80 %
投票人數:10人

A jagged array is an array of arrays such that member arrays can be of different sizes, i.e., we can create a 2-D array but with a variable ... Skiptocontent CoursesForWorkingProfessionalsLIVEDSALiveClassesSystemDesignJavaBackendDevelopmentFullStackLIVEExploreMoreSelf-PacedDSA-SelfPacedSDETheoryMust-DoCodingQuestionsExploreMoreForStudentsLIVECompetitiveProgrammingDataStructureswithC++DataScienceExploreMoreSelf-PacedDSA-SelfPacedCIPJAVA/Python/C++ExploreMoreSchoolCoursesSchoolGuidePythonProgrammingLearnToMakeAppsExploremoreAllCoursesTutorialsAlgorithmsAnalysisofAlgorithmsAsymptoticAnalysisWorst,AverageandBestCasesAsymptoticNotationsLittleoandlittleomeganotationsLowerandUpperBoundTheoryAnalysisofLoopsSolvingRecurrencesAmortizedAnalysisWhatdoes'SpaceComplexity'mean?Pseudo-polynomialAlgorithmsPolynomialTimeApproximationSchemeATimeComplexityQuestionSearchingAlgorithmsSortingAlgorithmsGraphAlgorithmsPatternSearchingGeometricAlgorithmsMathematicalBitwiseAlgorithmsRandomizedAlgorithmsGreedyAlgorithmsDynamicProgrammingDivideandConquerBacktrackingBranchandBoundAllAlgorithmsDataStructuresArraysLinkedListStackQueueBinaryTreeBinarySearchTreeHeapHashingGraphAdvancedDataStructureMatrixStringsAllDataStructuresInterviewCornerCompanyPreparationTopTopicsPracticeCompanyQuestionsInterviewExperiencesExperiencedInterviewsInternshipInterviewsCompetititveProgrammingDesignPatternsSystemDesignTutorialMultipleChoiceQuizzesLanguagesCC++JavaPythonC#JavaScriptjQuerySQLPHPScalaPerlGoLanguageHTMLCSSKotlinML&DataScienceMachineLearningDataScienceCSSubjectsMathematicsOperatingSystemDBMSComputerNetworksComputerOrganizationandArchitectureTheoryofComputationCompilerDesignDigitalLogicSoftwareEngineeringGATEGATEComputerScienceNotesLastMinuteNotesGATECSSolvedPapersGATECSOriginalPapersandOfficialKeysGATE2021DatesGATECS2021SyllabusImportantTopicsforGATECSWebTechnologiesHTMLCSSJavaScriptAngularJSReactJSNodeJSBootstrapjQueryPHPSoftwareDesignsSoftwareDesignPatternsSystemDesignTutorialSchoolLearningSchoolProgrammingMathematicsNumberSystemAlgebraTrigonometryStatisticsProbabilityGeometryMensurationCalculusMathsNotes(Class8-12)Class8NotesClass9NotesClass10NotesClass11NotesClass12NotesNCERTSolutionsClass8MathsSolutionClass9MathsSolutionClass10MathsSolutionClass11MathsSolutionClass12MathsSolutionRDSharmaSolutionsClass8MathsSolutionClass9MathsSolutionClass10MathsSolutionClass11MathsSolutionClass12MathsSolutionPhysicsNotes(Class8-11)Class8NotesClass9NotesClass10NotesClass11NotesChemistryNotesClass8NotesClass9NotesClass10NotesCSExams/PSUsISROISROCSOriginalPapersandOfficialKeysISROCSSolvedPapersISROCSSyllabusforScientist/EngineerExamUGCNETUGCNETCSNotesPaperIIUGCNETCSNotesPaperIIIUGCNETCSSolvedPapersStudentCampusAmbassadorProgramSchoolAmbassadorProgramProjectGeekoftheMonthCampusGeekoftheMonthPlacementCourseCompetititveProgrammingTestimonialsStudentChapterGeekontheTopInternshipCareersJobsApplyforJobsPostaJobHirewithUsKnowaboutJobathonJobathonPracticeAllDSAProblemsProblemoftheDayInterviewSeries:WeeklyContestsBi-WizardCoding:SchoolContestsContestsandEventsPracticeSDESheetCuratedDSAListsTop50ArrayProblemsTop50StringProblemsTop50TreeProblemsTop50GraphProblemsTop50DPProblems ComewritearticlesforusandgetfeaturedLearnandcodewiththebestindustryexpertsGetaccesstoad-freecontent,doubtassistanceandmore!ComeandfindyourdreamjobwithusGeeksDigestQuizzesGeeksCampusGblogArticlesIDECampusMantriHomeSavedVideosCoursesGBlogPuzzlesWhat'sNew? ChangeLanguage DataStructuresAlgorithmsInterviewPreparationTopic-wisePracticeC++JavaPythonCompetitiveProgrammingMachineLearningHTMLSDESheetPuzzlesGFGSchoolProjects RelatedArticles ▲RelatedArticlesBasicsofJavaJavaTutorialIntroductiontoJavaSimilaritiesandDifferencebetweenJavaandC++SettinguptheenvironmentinJavaJavaBasicSyntaxJavaHelloWorldProgramDifferencesbetweenJDK,JREandJVMHowJVMWorks–JVMArchitecture?JavaIdentifiersVariables&DataTypesinJavaVariablesinJavaScopeofVariablesInJavaDatatypesinJavaOperatorsinJavaOperatorsinJavaJavaArithmeticOperatorswithExamplesJavaAssignmentOperatorswithExamplesJavaUnaryOperatorwithExamplesJavaRelationalOperatorswithExamplesJavaLogicalOperatorswithExamplesJavaTernaryOperatorwithExamplesBitwiseOperatorsinJavaPackagesinJavaPackagesInJavaFlowControlinJavaDecisionMakinginJava(if,if-else,switch,break,continue,jump)JavaifstatementwithExamplesJavaif-elsestatementwithExamplesJavaif-else-ifladderwithExamplesLoopsinJavaLoopsinJavaJavaForloopwithExamplesJavawhileloopwithExamplesJavado-whileloopwithExamplesFor-eachloopinJavaJumpStatementsinJavaContinueStatementinJavaBreakstatementinJavareturnkeywordinJavaArraysinJavaArraysinJavaMultidimensionalArraysinJavaJaggedArrayinJavaStringsinJavaStringsinJavaStringclassinJava|Set1StringBufferclassinJavaStringBuilderClassinJavawithExamplesOOPSinJavaObjectOrientedProgramming(OOPs)ConceptinJavaClassesandObjectsinJavaMethodsinJavaAccessModifiersinJavaWrapperClassesinJavaNeedofWrapperClassesinJavaConstructorsinJavaConstructorsinJavaCopyConstructorinJavaConstructorChainingInJavawithExamplesPrivateConstructorsandSingletonClassesinJavaInheritance&PolymorphisminJavaInheritanceinJavaJavaandMultipleInheritanceComparisonofInheritanceinC++andJavaPolymorphisminJavaDynamicMethodDispatchorRuntimePolymorphisminJavaMethodoverloading&OveridingOverloadinginJavaDifferentwaysofMethodOverloadinginJavaOverridinginJavaDifferenceBetweenMethodOverloadingandMethodOverridinginJavaAbstraction&EncapsulationAbstractioninJavaAbstractClassesinJavaDifferencebetweenAbstractClassandInterfaceinJavaEncapsulationinJavaInterfacesinJavaInterfacesinJavaNestedInterfaceinJavaMarkerinterfaceinJavaFunctionalInterfacesinJavaComparatorInterfaceinJavawithExamplesKeywordsinJavaListofallJavaKeywordsSuperKeywordinJavafinalKeywordinJavaabstractkeywordinjavastaticKeywordinJava‘this’referenceinJavaenuminJavaExceptionHandlinginJavaExceptionsinJavaTypesofExceptioninJavawithExamplesCheckedvsUncheckedExceptionsinJavaTry,catch,throwandthrowsinJavaFlowcontrolintrycatchfinallyinJavathrowandthrowsinJavaUser-definedCustomExceptioninJavaCollectionFrameworkCollectionsinJavaCollectionsClassinJavaListInterfaceinJavawithExamplesArrayListinJavaVectorClassinJavaStackClassinJavaLinkedListinJavaQueueInterfaceInJavaPriorityQueueinJavaDequeinterfaceinJavawithExampleArrayDequeinJavaSetinJavaHashSetinJavaLinkedHashSetinJavawithExamplesSortedSetInterfaceinJavawithExamplesNavigableSetinJavawithExamplesTreeSetinJavaMapInterfaceinJavaHashMapinJavawithExamplesHashtableinJavaLinkedHashMapinJavaSortedMapInterfaceinJavawithExamplesTreeMapinJavaMulti-threadinginJavaMultithreadinginJavaLifecycleandStatesofaThreadinJavaMainthreadinJavaJavaThreadPriorityinMultithreadingThreadPoolsinJavaSynchronizationinJavaMethodandBlockSynchronizationinJavaImportanceofThreadSynchronizationinJavaThreadSafetyandhowtoachieveitinJavaBasicsofJavaJavaTutorialIntroductiontoJavaSimilaritiesandDifferencebetweenJavaandC++SettinguptheenvironmentinJavaJavaBasicSyntaxJavaHelloWorldProgramDifferencesbetweenJDK,JREandJVMHowJVMWorks–JVMArchitecture?JavaIdentifiersVariables&DataTypesinJavaVariablesinJavaScopeofVariablesInJavaDatatypesinJavaOperatorsinJavaOperatorsinJavaJavaArithmeticOperatorswithExamplesJavaAssignmentOperatorswithExamplesJavaUnaryOperatorwithExamplesJavaRelationalOperatorswithExamplesJavaLogicalOperatorswithExamplesJavaTernaryOperatorwithExamplesBitwiseOperatorsinJavaPackagesinJavaPackagesInJavaFlowControlinJavaDecisionMakinginJava(if,if-else,switch,break,continue,jump)JavaifstatementwithExamplesJavaif-elsestatementwithExamplesJavaif-else-ifladderwithExamplesLoopsinJavaLoopsinJavaJavaForloopwithExamplesJavawhileloopwithExamplesJavado-whileloopwithExamplesFor-eachloopinJavaJumpStatementsinJavaContinueStatementinJavaBreakstatementinJavareturnkeywordinJavaArraysinJavaArraysinJavaMultidimensionalArraysinJavaJaggedArrayinJavaStringsinJavaStringsinJavaStringclassinJava|Set1StringBufferclassinJavaStringBuilderClassinJavawithExamplesOOPSinJavaObjectOrientedProgramming(OOPs)ConceptinJavaClassesandObjectsinJavaMethodsinJavaAccessModifiersinJavaWrapperClassesinJavaNeedofWrapperClassesinJavaConstructorsinJavaConstructorsinJavaCopyConstructorinJavaConstructorChainingInJavawithExamplesPrivateConstructorsandSingletonClassesinJavaInheritance&PolymorphisminJavaInheritanceinJavaJavaandMultipleInheritanceComparisonofInheritanceinC++andJavaPolymorphisminJavaDynamicMethodDispatchorRuntimePolymorphisminJavaMethodoverloading&OveridingOverloadinginJavaDifferentwaysofMethodOverloadinginJavaOverridinginJavaDifferenceBetweenMethodOverloadingandMethodOverridinginJavaAbstraction&EncapsulationAbstractioninJavaAbstractClassesinJavaDifferencebetweenAbstractClassandInterfaceinJavaEncapsulationinJavaInterfacesinJavaInterfacesinJavaNestedInterfaceinJavaMarkerinterfaceinJavaFunctionalInterfacesinJavaComparatorInterfaceinJavawithExamplesKeywordsinJavaListofallJavaKeywordsSuperKeywordinJavafinalKeywordinJavaabstractkeywordinjavastaticKeywordinJava‘this’referenceinJavaenuminJavaExceptionHandlinginJavaExceptionsinJavaTypesofExceptioninJavawithExamplesCheckedvsUncheckedExceptionsinJavaTry,catch,throwandthrowsinJavaFlowcontrolintrycatchfinallyinJavathrowandthrowsinJavaUser-definedCustomExceptioninJavaCollectionFrameworkCollectionsinJavaCollectionsClassinJavaListInterfaceinJavawithExamplesArrayListinJavaVectorClassinJavaStackClassinJavaLinkedListinJavaQueueInterfaceInJavaPriorityQueueinJavaDequeinterfaceinJavawithExampleArrayDequeinJavaSetinJavaHashSetinJavaLinkedHashSetinJavawithExamplesSortedSetInterfaceinJavawithExamplesNavigableSetinJavawithExamplesTreeSetinJavaMapInterfaceinJavaHashMapinJavawithExamplesHashtableinJavaLinkedHashMapinJavaSortedMapInterfaceinJavawithExamplesTreeMapinJavaMulti-threadinginJavaMultithreadinginJavaLifecycleandStatesofaThreadinJavaMainthreadinJavaJavaThreadPriorityinMultithreadingThreadPoolsinJavaSynchronizationinJavaMethodandBlockSynchronizationinJavaImportanceofThreadSynchronizationinJavaThreadSafetyandhowtoachieveitinJavaJaggedArrayinJavaViewDiscussion ImproveArticle SaveArticle LikeArticle DifficultyLevel: EasyLastUpdated: 15Nov,2021Prerequisite:ArraysinJavaAjaggedarrayisanarrayofarrayssuchthatmemberarrayscanbeofdifferentsizes,i.e.,wecancreatea2-Darraybutwithavariablenumberofcolumnsineachrow.ThesetypesofarraysarealsoknownasJaggedarrays. PictorialrepresentationofJaggedarrayinMemory:Jagged_array DeclarationandInitializationofJaggedarray:Syntax:data_typearray_name[][]=newdata_type[n][];//n:no.ofrows array_name[]=newdata_type[n1]//n1=no.ofcolumnsinrow-1 array_name[]=newdata_type[n2]//n2=no.ofcolumnsinrow-2 array_name[]=newdata_type[n3]//n3=no.ofcolumnsinrow-3 . . . array_name[]=newdata_type[nk]//nk=no.ofcolumnsinrow-n Alternative,waystoInitializeaJaggedarray:intarr_name[][]=newint[][]{ newint[]{10,20,30,40}, newint[]{50,60,70,80,90,100}, newint[]{110,120} }; OR int[][]arr_name={ newint[]{10,20,30,40}, newint[]{50,60,70,80,90,100}, newint[]{110,120} }; OR int[][]arr_name={ {10,20,30,40}, {50,60,70,80,90,100}, {110,120} };FollowingareJavaprogramstodemonstratetheaboveconcept. Java//Programtodemonstrate2-DjaggedarrayinJavaclassMain{    publicstaticvoidmain(String[]args)    {        //Declaring2-Darraywith2rows        intarr[][]=newint[2][];         //MakingtheabovearrayJagged         //Firstrowhas3columns        arr[0]=newint[3];         //Secondrowhas2columns        arr[1]=newint[2];         //Initializingarray        intcount=0;        for(inti=0;i



請為這篇文章評分?