java 7
java 7
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.
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:
an Arrays.
Search in an Arrays.
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
String s = “
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.
● 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 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.
Declaration – Syntax:
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.
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().