Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • Java Arrays
  • Java Strings
  • Java OOPs
  • Java Collection
  • Java 8 Tutorial
  • Java Multithreading
  • Java Exception Handling
  • Java Programs
  • Java Project
  • Java Collections Interview
  • Java Interview Questions
  • Java MCQs
  • Spring
  • Spring MVC
  • Spring Boot
  • Hibernate
Open In App
Next Article:
Array setInt() method in Java
Next article icon

Array set() method in Java

Last Updated : 10 Nov, 2022
Comments
Improve
Suggest changes
Like Article
Like
Report

The java.lang.reflect.Array.set() is an inbuilt method in Java and is used to set a specified value to a specified index of a given object array.

Syntax

  Array.set(Object []array, int index, Object value)  

Parameter :

  • array : This is an array of type Object which is to be updated.
  • index : This is the index of the array which is to be updated.
  • value : This is the value that is to be set at the given index of the given array

Return type : This is a void type method this doesn’t returns any value. The update reflects upon the Object array passed as the argument.

Exception : This method throws following exception.

  • NullPointerException – when the array is null.
  • IllegalArgumentException – when the given object array is not an Array.
  • ArrayIndexOutOfBoundsException – if the given index is not in the range of the size of the array.
  • Below programs illustrate the Array.set() method:

    Program 1 :




    // Java code to demonstrate set() method of Array class
    import java.lang.reflect.Array;
    public class GfG {
        // main method
        public static void main(String[] args)
        {
      
            // Declaring and defining a String array
            String s[] = { "Geeks", "is", "Geeks" };
      
            System.out.print("Before Set : ");
            // printing the array
            for (String x : s) {
                System.out.print(x);
            }
      
            // set method of class Array
            Array.set(s, 1, "for");
      
            System.out.print("\nAfter Set : ");
            // printing array
            for (String x : s) {
                System.out.print(x);
            }
        }
    }
     
     
    Output:
      Before Set : GeeksisGeeks  After Set : GeeksforGeeks  

    Program 2 : to demonstrate java.lang.NullPointerException




    // Java code to demonstrate set() method of Array class
    import java.lang.reflect.Array;
    public class GfG {
        // main method
        public static void main(String[] args)
        {
      
            // Declaring and defining a String array
            String s[] = null;
      
            try {
                // set method of class Array
                Array.set(s, 1, "for");
            }
            catch (Exception e) {
                System.out.println("Exception : " + e);
            }
        }
    }
     
     
    Output:
      Exception : java.lang.NullPointerException  

    Program 3 : to demonstrate java.lang.ArrayIndexOutOfBoundsException




    // Java code to demonstrate set() method of Array class
    import java.lang.reflect.Array;
    public class GfG {
        // main method
        public static void main(String[] args)
        {
      
            // Declaring and defining a String array
            String s[] = { "Geeks", "for", "Geeks" };
      
            try {
                // set method of class Array
                Array.set(s, 4, "for");
            }
            catch (Exception e) {
                System.out.println("Exception : " + e);
            }
        }
    }
     
     
    Output:
      Exception : java.lang.ArrayIndexOutOfBoundsException  

    Program 4 : to demonstrate java.lang.IllegalArgumentException




    // Java code to demonstrate set() method of Array class
    import java.lang.reflect.Array;
    public class GfG {
        // main method
        public static void main(String[] args)
        {
      
            // Declaring and defining a String array
            String s = "GeeksforGeeks";
      
            try {
                // set method of class Array
                Array.set(s, 4, "for");
            }
            catch (Exception e) {
                System.out.println("Exception : " + e);
            }
        }
    }
     
     
    Output:
      Exception : java.lang.IllegalArgumentException: Argument is not an array  


    Next Article
    Array setInt() method in Java

    S

    ShivamKD
    Improve
    Article Tags :
    • Java
    • Java-Arrays
    • Java-Collections
    • Java-Functions
    • java-lang-reflect-package
    • java-reflection-array
    Practice Tags :
    • Java
    • Java-Collections

    Similar Reads

    • Array setInt() method in Java
      The java.lang.reflect.Array.setInt() is an inbuilt method in Java and is used to set a specified int value to a specified index of a given object array. Syntax: Array.setInt(Object []array, int index, int value) Parameter: array: This is an array of type Object which is to be updated. index: This is
      3 min read
    • Array setByte() method in Java
      The java.lang.reflect.Array.setByte() is an inbuilt method in Java and is used to set a specified byte value to a specified index of a given object array. Syntax: Array.setByte(Object []array, int index, byte value) Parameter: This method takes 3 parameters: array: This is an array of type Object wh
      3 min read
    • Array setLong() method in Java
      The java.lang.reflect.Array.setLong() is an inbuilt method in Java and is used to set a specified long value to a specified index of a given object array. Syntax: Array.setLong(Object []array, int index, long value) Parameter: array : This is an array of type Object which is to be updated. index : T
      3 min read
    • Array setChar() method in Java
      The java.lang.reflect.Array.setChar() is an inbuilt method in Java and is used to change a specified char value to a specified index of a given object array. Syntax: Array.setChar(Object []array, int index, char value) Parameter: This method takes three parameters: array: This is an array of type Ob
      3 min read
    • Array setShort() method in Java
      The java.lang.reflect.Array.setShort() is an inbuilt method in Java and is used to set a specified short value to a specified index of a given object array. Syntax: Array.setShort(Object []array,int index, short value) Parameters: This method takes 3 parameters: array: This is an array of type Objec
      3 min read
    • Array setFloat() method in Java
      The java.lang.reflect.Array.setFloat() is an inbuilt method in Java and is used to change a specified float value to a specified index of a given object array. Syntax: Array.setFloat(Object []array, int index, float value) Parameter: This method takes three parameters: array: This is an array of typ
      3 min read
    • Java ArrayList set() Method
      The set() method of the ArrayList class in Java is used to replace an element at a specified position with a new value. This is very useful when we need to update an existing element in an ArrayList while maintaining the list's structure. Example 1: Here, we will use the set() method to update an el
      2 min read
    • Array setDouble() method in Java
      The java.lang.reflect.Array.setDouble() is an inbuilt method in Java and is used to set a specified double value to a specified index of a given object array. Syntax: Array.setDouble(Object []array, int index, double value) Parameter: This method takes three parameters: array: This is an array of ty
      3 min read
    • Arrays.stream() Method in Java
      Arrays.stream() method is used to get a sequential Stream from the elements of an array passed as a parameter. Below is a simple example that uses Arrays.stream() to convert a String array into a Stream for sequential processing. Example: [GFGTABS] Java // Java program to demonstrate Arrays.stream()
      3 min read
    • ArrayDeque size() Method in Java
      The Java.util.ArrayDeque.size() method in Java is used to get the size of the Deque or the number of elements present in the Deque. Syntax: Array_Deque.size() Parameters: The method does not take any parameter. Return Value: The method returns the size or the number of elements present in the Deque.
      2 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences