Hub Of Geekz

  • Home
  • About Me
  • Contact Us
  • Home
  • Languages
    • C++
    • Java
    • Perl
    • Prolog
    • Bootstrap
  • Database
    • SQL
    • PL/SQL
  • Study
    • Java
      • Java Collection
      • Java Concurrency
      • Java Interview Questions
      • Java Networking
    • Number System
  • Kavita
  • Entertainment
    • Hinglish Cafe
    • Videos
    • Jokes
  • Windows Tricks
  • How To

Tuesday, 10 March 2015

Arrays Class in Java

 Earthcare Foundation NGO     10:11     Java     No comments   

In this post i will explain about the Arrays class in Java which is inbuilt and we can use many predefined methods on this Array class which makes our problem simple.
1)sort():This method is used to sort the array elements.
2)equal():This is a method for comparing two arrays.
3)binarySearch():This is a method to find an element using binary search.This method returns the location of the element.
4)fill():This is a method to assign specified value to each element of specified  array.
Arrays.fill(arr,7);
where arr is an array of type integer,then it will replace all the elements in the array with 7.
5)arrayCopy():This is a method by which we can copy elements of one array to another.

Program Code:

package examples;

import java.util.Arrays;

public class ArraysClass {

public static void main(String[] args) {

int[] arr={2,5,3,6,8,65,23,45};
System.out.println("Elements of arr are ");
for(int element:arr){
System.out.println(element);
}
Arrays.sort(arr);
System.out.println("Sorted array is ");
for(int element:arr){
System.out.println(element);
}
int[] arr1=new int[6];
Arrays.fill(arr1, 7);
System.out.println("Elements of array after insertion  are ");
for(int element:arr1){
System.out.println(element);
}
int [] arr2={1,4,6,8,9};
System.out.println("Elements of arr1 are ");
for(int element:arr2){
System.out.println(element);
}
System.arraycopy(arr2, 0, arr1, 0, arr2.length);
System.out.println("Elements of arr1 after copy ");
for(int element:arr1){
System.out.println(element);
}
int loc=Arrays.binarySearch(arr2, 3);
if(loc>=0){
System.out.println("Element 3 is found in arr2 at location "+(loc+1));
}else{
System.out.println("Element 3 is not present in the arr2");
}
loc=Arrays.binarySearch(arr2, 4);
if(loc>=0){
System.out.println("Element 4 is found in arr2 at location "+(loc+1));
}else{
System.out.println("Element 4 is not present in the arr2");
}
boolean equ=Arrays.equals(arr, arr1);
if(equ){
System.out.println("Both array arr and arr1 are same");
}else{
System.out.println("Arrays arr and arr1 are not same");
}
}

}

Output:
Elements of arr are
2
5
3
6
8
65
23
45
Sorted array is
2
3
5
6
8
23
45
65
Elements of array after insertion  are
7
7
7
7
7
7
Elements of arr1 are
1
4
6
8
9
Elements of arr1 after copy
1
4
6
8
9
7
Element 3 is not present in the arr2
Element 4 is found in arr2 at location 2
Arrays arr and arr1 are not same

One thing to note here is that in method,
System.arraycopy(srcArray,startIndex,dstArray,startIndex,length);
if we provide length which is larger than srcArray then we will get an ArrayIndexOutOfBoundsException.
So above is all about Arrays class in Java.If there is any query let me know by comments or by email.
Thanks.
  • Share This:  
  •  Facebook
  •  Twitter
  •  Google+
  •  Stumble
  •  Digg
Email ThisBlogThis!Share to XShare to Facebook
Newer Post Older Post Home

0 comments:

Post a Comment

Ad


Jobsmag.inIndian Education BlogThingsGuide

Subscribe

Do you want fresh and hot updates about us? Then subscribe to our Feeds.

Total Pageviews

Popular Posts

  • Write a program in PL/SQL to print the factorial of a number.
    In this post I will explain how to get the factorial of any given number. For that first you need to know what is the procedure to find ...
  • To find the GCD of two numbers in PROLOG.
    gcd(X,Y):-X=Y,write('GCD of two numbers is '),write(X); X=0,write('GCD of two numbers is '),write(Y); Y=0,write('G...
  • Write a PL/SQL code to get the Fibonacci Sequence
    First, I will explain what is Fibonacci Sequence and how to get this series. So, Fibonacci Sequence is a series of numbers 0,1,1,2,3,5,8,1...

Label

All Articles Best Resources Blogging Boost Traffic Bootstrap C Plus Plus Collection Comedy Comedy Posts Comedy Videos Concurrency creative commons website Education Employee Entertainment Fibonacci Sequence free images GirlFriend Hinglish Cafe How To Image Websites Inspirational Java Java Interview Questions Java Networking Kavita Sangrah Life Lock Sreen Love Number System Patterns Perl Picture PL/SQL Plastic Engineering Programming Prolog public domain SEO Servlet Short Story Shortcut Keys Social Media Social Services SQL SuVichar Thread Traffic True Events Ultimate Guide Windows Tricks Windows8.1 WordPress

Blog Archive

  • ►  2020 (43)
    • ►  September (41)
    • ►  August (2)
  • ►  2019 (1)
    • ►  July (1)
  • ►  2018 (9)
    • ►  September (7)
    • ►  July (1)
    • ►  May (1)
  • ►  2017 (8)
    • ►  June (3)
    • ►  May (3)
    • ►  March (1)
    • ►  January (1)
  • ►  2016 (2)
    • ►  September (1)
    • ►  January (1)
  • ▼  2015 (91)
    • ►  December (1)
    • ►  November (1)
    • ►  October (6)
    • ►  May (10)
    • ▼  March (20)
      • ArrayList Methods in Java
      • ArrayList Methods in Java
      • ArrayList in Java
      • Arrays Class in Java
      • Java Interview Question-Variable length arguments ...
      • Array in Java
      • Data Hierarchy in Operating Systems
      • Synchronization in Java
      • Typecasting in Java
      • Concurrency in Java
      • Runnable in Java
      • HashMap in Java
      • Reverse of a given number using Java
      • Star Pattern printing in Java
      • Difference between fail-fast and fail-safe iterators
      • Factorial of a number in Java
      • Null in Java
      • Final in Java
      • Immutable Objects in Java
      • Thread concept in java
    • ►  February (50)
    • ►  January (3)
  • ►  2014 (339)
    • ►  December (1)
    • ►  October (55)
    • ►  September (58)
    • ►  August (94)
    • ►  July (64)
    • ►  June (67)
  • ►  2013 (34)
    • ►  August (5)
    • ►  April (29)
  • ►  2012 (20)
    • ►  November (1)
    • ►  October (15)
    • ►  September (4)

Author

  • Earthcare Foundation NGO
  • Kavita house
  • Unknown
  • Unknown

Info

Copyright © Hub Of Geekz | Powered by Blogger
Design by Hardeep Asrani | Blogger Theme by NewBloggerThemes.com | Distributed By Gooyaabi Templates