0% found this document useful (0 votes)
3 views4 pages

java 7

The document discusses the Java Arrays class and String class, highlighting their methods and usage. It explains the utility of the Arrays class for operations like filling, sorting, and searching arrays, while also detailing the immutability of String objects and different ways to create them. Additionally, it covers the introduction of StringJoiner in Java 8 for easier string concatenation with delimiters.

Uploaded by

tejushetty547
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views4 pages

java 7

The document discusses the Java Arrays class and String class, highlighting their methods and usage. It explains the utility of the Arrays class for operations like filling, sorting, and searching arrays, while also detailing the immutability of String objects and different ways to create them. Additionally, it covers the introduction of StringJoiner in Java 8 for easier string concatenation with delimiters.

Uploaded by

tejushetty547
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

OBJECT ORIENTED PROGRAMMING AND DESIGN WITH JAVA (20CS43P)

WEEK:7
7.study and report.
a.java arrays class their methodsb.java string
class their methods
The Arrays class in java.util package is a part of the Java Collection Framework. This class
provides static methods to dynamically create and access Java arrays. It consists ofonly static
methods and the methods of Object class. The methods of this class can be used by the class name
itself.

The class hierarchy is as follows:

java.lang.Object
java.util.Arrays

why do we need java Arrays class when we are able to declare, initialize and compute operations
over arrays. The answer to this though lies within the methods of this class which we are going to
discuss further as practically these functions help programmersexpanding horizons with arrays
for instance there are often times when are used to do some tasks on an array like:

Fill an array with a particular value.Sort

an Arrays.

Search in an Arrays.

And many more.

String class in Java

The string is a sequence of characters. In Java, objects of String are immutable whichmeans a
constant and cannot be changed once created.
Creating a String

COMPUTER SCIENCE AND ENGINEERING Page 21


OBJECT ORIENTED PROGRAMMING AND DESIGN WITH JAVA (20CS43P)

There are two ways to create string in Java:


1. String literal

String s = “

2. Using new keyword


String s = new String

String Constructors in Java


1. String(byte[] byte_arr)

Construct a new String by decoding the byte array. It uses the platform's default
character set for decoding.

Example:
arrays are used to store the data in a tabular form. For example, storing the roll numberand marks of a
student can be easily done using multidimensional arrays. Another common usage is to store the
images in 3D arrays.

● In dynamic programming questions, multidimensional arrays are used which areused to


represent the states of the problem.

● Apart from these, they also have applications in many standard algorithmic problems like:
Matrix Multiplication, Adjacency matrix representation in graphs, Gridsearch problems

Two – dimensional Array (2D-Array)

Two – dimensional array is the simplest form of a multidimensional array. A two –dimensional array
can be seen as an array of one – dimensional array for easier understanding.

Indirect Method of Declaration:

Declaration – Syntax:

data_type[][] array_name = new data_type[x][y];


For example: int[][] arr = new int[10][20];

COMPUTER SCIENCE AND ENGINEERING Page 22


OBJECT ORIENTED PROGRAMMING AND DESIGN WITH JAVA (20CS43P)

array_name[row_index][column_index] = value;
For example: arr[0][0] = 1;

Prior to Java 8 when we need to concatenate a group of strings we need to write that code
manually in addition to this we needed to repeatedly use delimiter and sometimes it leads to
several mistakes but after Java 8 we can concatenate the strings using StringJoiner class and
String.join() method then we can easily achieveour goal.

Example: Without StringJoiner Class and without String.join() methodApproach :


In this program we will not use java 8 we will try to do it manually and try to understand
for this simple operation of the concatenation of strings with delimiterhow much bigger
code/steps we needed to achieve our goal.
First, we will join three strings “DSA”, “FAANG”, “ALGO”, and the delimiter will be
“for doing this we need to write delimiter every time before adding the next string.

Next, we will join elements of an ArrayList containing strings “DSA”,”FAANG”, “ALGO”


and delimiter will be and for doing this we needed to iterate over ArrayListand then add
strings using a delimiter.

Note here for i=0 we directly assigned the string joined2 to the first element of ArrayList
because if we didn’t put this condition then we will get the output like this ”gfg DSA gfg
FAANG gfg ALGO” whereas the actual output did not have a delimiter before the first
element of ArrayList “DSA gfg FAANG gfg ALGO”.
As from the code you can see how much code is written for doing this simple task for doing
task now we will look for using StringJoiner class and String.join() method.
StringJoiner is used to construct a sequence of characters separated by a delimiterand
optionally starting with a supplied prefix and ending with a supplied suffix. In simple words
in the constructor of StringJoiner calls we can pass Three parametersdelimiter, suffix,
prefix. Prefix be added to the start of a String and suffix will be at the end. For adding the
strings we will need to simply call the add() method on the StringJoiner class.

Approach:
If you are adding a group of strings you need to create StringJoiner class Object and pass
delimiter, prefix, suffix.Here the delimiter is “gfg ” string and the prefix is “[” and suffix is “]”.
Now we need to add strings in StringJoiner class using add() method.Finally,we will convert the
StringJoiner object to a String object using to String().

COMPUTER SCIENCE AND ENGINEERING Page 23


OBJECT ORIENTED PROGRAMMING AND DESIGN WITH JAVA (20CS43P)

COMPUTER SCIENCE AND ENGINEERING Page 24

You might also like