COURSES
MBAData Science & AnalyticsDoctorate Software & Tech AI | ML MarketingManagement
Professional Certificate Programme in HR Management and AnalyticsPost Graduate Certificate in Product ManagementExecutive Post Graduate Program in Healthcare ManagementExecutive PG Programme in Human Resource ManagementMBA in International Finance (integrated with ACCA, UK)Global Master Certificate in Integrated Supply Chain ManagementAdvanced General Management ProgramManagement EssentialsLeadership and Management in New Age BusinessProduct Management Online Certificate ProgramStrategic Human Resources Leadership Cornell Certificate ProgramHuman Resources Management Certificate Program for Indian ExecutivesGlobal Professional Certificate in Effective Leadership and ManagementCSM® Certification TrainingCSPO® Certification TrainingLeading SAFe® 5.1 Training (SAFe® Agilist Certification)SAFe® 5.1 POPM CertificationSAFe® 5.1 Scrum Master Certification (SSM)Implementing SAFe® 5.1 with SPC CertificationSAFe® 5 Release Train Engineer (RTE) CertificationPMP® Certification TrainingPRINCE2® Foundation and Practitioner Certification
Law
Job Linked
Bootcamps
Study Abroad
Master of Business Administration (90 ECTS)Master of Business Administration (60 ECTS)Master in Computer Science (120 ECTS)Master in International Management (120 ECTS)Bachelor of Business Administration (180 ECTS)B.Sc. Computer Science (180 ECTS)MS in Data AnalyticsMS in Project ManagementMS in Information TechnologyMasters Degree in Data Analytics and VisualizationMasters Degree in Artificial IntelligenceMBS in Entrepreneurship and MarketingMSc in Data AnalyticsMS in Data AnalyticsMaster of Science in AccountancyMS in Computer ScienceMaster of Science in Business AnalyticsMaster of Business Administration MS in Data ScienceMS in Information TechnologyMaster of Business AdministrationMS in Applied Data ScienceMaster of Business AdministrationMS in Data AnalyticsM.Sc. Data Science (60 ECTS)Master of Business AdministrationMS in Information Technology and Administrative Management MS in Computer Science Master of Business Administration MBA General Management-90 ECTSMSc International Business ManagementMS Data Science MBA Business Technologies MBA Leading Business Transformation Master of Business Administration MSc Business Intelligence and Data ScienceMS Data Analytics MS in Management Information SystemsMSc International Business and ManagementMS Engineering ManagementMS in Machine Learning EngineeringMS in Engineering ManagementMSc Data EngineeringMSc Artificial Intelligence EngineeringMPS in InformaticsMPS in Applied Machine IntelligenceMS in Project ManagementMPS in AnalyticsMS in Project ManagementMS in Organizational LeadershipMPS in Analytics - NEU CanadaMBA with specializationMPS in Informatics - NEU Canada Master in Business AdministrationMS in Digital Marketing and MediaMS in Project ManagementMaster in Logistics and Supply Chain ManagementMSc Sustainable Tourism and Event ManagementMSc in Circular Economy and Sustainable InnovationMSc in Impact Finance and Fintech ManagementMS Computer ScienceMS in Applied StatisticsMS in Computer Information SystemsMBA in Technology, Innovation and EntrepreneurshipMSc Data Science with Work PlacementMSc Global Business Management with Work Placement MBA with Work PlacementMS in Robotics and Autonomous SystemsMS in Civil EngineeringMS in Internet of ThingsMSc International Logistics and Supply Chain ManagementMBA- Business InformaticsMSc International ManagementMS Computer Science with AIML ConcentrationMBA in Strategic Data Driven ManagementMaster of Business AdministrationMSc Digital MarketingMBA Business and MarketingMaster of Business AdministrationMSc Digital MarketingMSc in Sustainable Luxury and Creative IndustriesMSc in Sustainable Global Supply Chain ManagementMSc in International Corporate FinanceMSc Digital Business Analytics MSc in International HospitalityMSc Luxury and Innovation ManagementMaster of Business Administration-International Business ManagementMS in Computer EngineeringMS in Industrial and Systems EngineeringMSc International Business ManagementMaster in ManagementMSc MarketingMSc Business ManagementMSc Global Supply Chain ManagementMS in Information Systems and Technology with Business Intelligence and Analytics ConcentrationMSc Corporate FinanceMSc Data Analytics for BusinessMaster of Business AdministrationMaster of Business Administration 60 ECTSMaster of Business Administration 90 ECTSMaster of Business Administration 60 ECTSMaster of Business Administration 90 ECTSBachelors in International Management
For College Students

What is Array in Java?

$$/$$

This segment will teach you how you can store more than one value in a single variable. This is done using “Arrays”. You will now learn how to declare these arrays and how to use them.

$$/$$

Video Transcript

 

So till now you have played around a little with different kinds of data types, seen some arithmetic operations, casting and also seen how strings and characters work. Now let us move on to something different. Suppose that I have the list of all the students in my class who are enrolled in a particular course. So for example, I have these four students us. Now, if I wish to store the names of these four students, one way to do this would be to store them in different variables. So I would say string s one equals to Adam, s two is Lucy, s three is Emma and s four is John. So now, if I would find a way to accumulate all these four names and store them in a single collection of these strings, it would be far more beneficial. And why would it be beneficial? Well, if I could collect all these four names at one place and store them inside a single object, then it would make my life definitely easier because then I would not have to remember all these variable names. So basically, earlier we read that these variables are nothing but different small containers which contains these values which are the names of these students. Now, if somehow I could store these four small containers inside a big container, then it would make my life easier, because then I would not need to remember the names of these four small containers, and I could simply assign a single name to this particular big container and then try to retrieve the names of the students from here. So basically, arrays is one data type which help us to accumulate different kinds of objects all at one place. The advantage of arrays is that if I have say, a list of 100 students, then I don't need to declare 100 variables. For this I can simply declare just one variable. That variable will be my array. So basically another analogy which we can think of is that suppose a teacher comes into the classroom and the teacher wishes to take the attendance role of the students. So wouldn't it be better if the teacher would have the list of all the hundred students in the class written down on one single page of the notebook? Then the teacher could simply move from one name to another, role number wise, and take the attendance. So basically, an array is nothing but a list of objects which are made of the same data type. That is, I can declare an array which would store strings inside it, or I can declare an array which would store numbers inside it. Suppose I'm conducting a scientific experiment and I get some readings. Then I can also declare an array which would store those readings inside that particular array. So in cases such as scientific experiments, there might be hundreds and thousands of values. So instead of declaring 1000 different variables, I can simply store them all at one place in my array collection. So here basically in my array, I can construct four boxes like this and store these four strings inside it. I can assign values to all these four boxes and I can retrieve their values by using an internal numbering system. So all the arrays in Java have an internal numbering system. For example, if I have four elements inside my array, then the numbering will begin from zero, will increase by one, two and three. So basically, if I have an array of four objects, the numbering which in Java we call as the indexing will always begin from zero and will keep on getting incremented one by one. Another important thing to notice is that for a single array it can only contain different objects which belong to the same data type. For example, if this object is a string, then all the objects inside this array should all be strings. Otherwise it would throw up an error. Or if this array consists of numbers only, then all of these objects must all be numbers. So now let us learn how you can declare a new array and probably assign values to them. So, for declaring an array, I would say can we first think of a name for our array? Suppose this is my big container here in which I wish to store these four names. Now suppose I wish to call this particular container students.

 

So if I want my array to be called as students and I wish that only there should be strings inside this array, I would say string. So this basically tells the computer that I am about to assign string values to this array. Then I say students. This denotes the name of the array. And another careful thing is that after students make these two square brackets so this statement basically string students followed by opening and closed square bracket denotes that this variable, students is actually an array only of strings.

 

Video Recap

 

  • Introduction to data types, arithmetic operations, casting, strings, and characters

  • The benefits of storing multiple objects in a single collection using arrays

  • Arrays allow for easier management and organization of data, particularly with large sets of objects

  • Arrays are lists of objects with the same data type, such as strings or numbers

  • The internal numbering system for arrays begins at zero and increases by one for each element

  • Arrays can only contain objects of the same data type

  • Declaration of a new array involves choosing a name and specifying the data type and brackets to denote an array

  • Example of an array for storing student names as strings is provided.

 

 

Note that all the elements in an array can only be of one data type. i.e if an array is declared as String, only string type data can be stored at each index and no other data type is compatible.

Now that you know how to declare an array of a particular data type. In the following video, you will learn about the process of storing data in an array.

$$/$$

You have learned how you can use arrays to store multiple values in a variable. You write the data type first, followed by the name of the array, followed by square brackets.

To initialize an array, you can do this:

<array name> = new <data type>[<number of elements in the array>];

For example, names=new String[4];

 

Also, each element in an array can be accessed using its index. The index starts from 0 and not 1; this is something important that you need to remember. You will find arrays very useful when you reach the Functions module.

So, to store data at each index, you can do this;

<array name>[<index>]=<data>;

For example; names[1]=”Aishwarya”;

$$/$$

There is another way to declare and initialize an array and store elements in it. Let us take a look at this simpler and faster method.

$$/$$

As you already saw, there is another way to declare and initialize an array in the same line.

For example:

String names[]={“Ankit”,”Aishwarya”,”Ifrah”}

 

Let us now try to run all the new statements you learned about and observe the results.

You can download the following java file which will be very useful to you while watching the video. Right click on the java file and open it with Intellij. Please be careful with the name of the java file. The name of the java file should exactly be same as the class name inside it for the code to run. So make changes accordingly.

$$/$$

In the above video, you saw how to declare and initialize arrays. Also, you saw what happens when you try to access an index in an array which does not exist within bounds of the array.

$$/$$

Till now, you have learned to store and manipulate data which is in the form of numbers or characters.

In the next segment, you will learn about operations done on boolean variables which store only true/false values.