Java Articles

Page 346 of 450

What does the method removeRange(int fromIndex, int toIndex) do in java?

Srinivas Gorla
Srinivas Gorla
Updated on 20-Feb-2020 190 Views

The removeRange() method of the ArrayList class removes all of the elements from this List whose index is between fromIndex and toIndex.Exampleimport java.util.*; public class ArrayListDemo extends ArrayList{    public static void main(String[] args) {       ArrayListDemo arrlist = new ArrayListDemo();       arrlist.add(10);       arrlist.add(12);       arrlist.add(31);       System.out.println("The list:" + arrlist);       arrlist.removeRange(0,2);       System.out.println("The list after using removeRange:" + arrlist);    } }OutputThe list:[10, 12, 31] The list after using removeRange:[31]

Read More

What does the method set(int, obj o) do in java?

Abhinanda Shri
Abhinanda Shri
Updated on 20-Feb-2020 172 Views

The set() method of the ArrayList class replaces the element at the specified position in this list with the specified element.Exampleimport java.util.ArrayList; public class Sample {    public static void main(String args[]) {       ArrayList al = new ArrayList();       System.out.println("Initial size of al: " + al.size());       al.add("C");       al.add("A");       al.add("E");       al.add(1, "A2");       System.out.println("Size of al after additions: " + al.size());       System.out.println("Contents of al: " + al);       System.out.println("Size of al after deletions: " + al.size());     ...

Read More

What does the method indexOf(obj o) do in java?

Sravani S
Sravani S
Updated on 20-Feb-2020 214 Views

The indexOf(Object) method of the java.util.ArrayList class returns the index of the first occurrence of the specified element in this list, or -1 if this list does not contain the element.Exampleimport java.util.ArrayList; public class ArrayListDemo {    public static void main(String[] args) {       ArrayList arrlist = new ArrayList(5);       arrlist.add("G");       arrlist.add("E");       arrlist.add("F");       arrlist.add("M");       System.out.println("Size of list: " + arrlist.size());       for (String value : arrlist) {          System.out.println("Value = " + value);       }     ...

Read More

What does the method size() do in java?

Abhinanda Shri
Abhinanda Shri
Updated on 20-Feb-2020 898 Views

The size() method of the class java.util.ArrayList returns the number of elements in this list i.e. the size of the list.Exampleimport java.util.ArrayList; public class ArrayListDemo {    public static void main(String[] args) {       ArrayList arrlist = new ArrayList(5);       arrlist.add(15);       arrlist.add(20);       arrlist.add(25);       arrlist.add(22);       for (Integer number : arrlist) {          System.out.println("Number = " + number);       }       int retval = arrlist.size();       System.out.println("Size of list = " + retval);    } }OutputNumber = 15 Number = 20 Number = 25 Number = 22 Size of list = 4

Read More

What does the method remove(obj o) do in java?

Sravani S
Sravani S
Updated on 20-Feb-2020 189 Views

The remove(Object) method of the class java.util.ArrayList removes the first occurrence of the specified element from this list, if it is present. If the list does not contain the element, it is unchanged.Exampleimport java.util.ArrayList; public class ArrayListDemo {    public static void main(String[] args) {       ArrayList arrlist = new ArrayList(5);       arrlist.add("G");       arrlist.add("E");       arrlist.add("F");       arrlist.add("M");       arrlist.add("E");       System.out.println("Size of list: " + arrlist.size());       for (String value : arrlist) {          System.out.println("Value = " + ...

Read More

How to truncate a file in Java?

Swarali Sree
Swarali Sree
Updated on 20-Feb-2020 2K+ Views

The flush() method of the FileWriter class flushes the contents of the file. You can use this method to truncate a file.Exampleimport java.io.File; import java.io.FileWriter; public class FileTruncate { public static void main(String args[]) throws Exception { File file = new File("myData"); FileWriter fw = new FileWriter(file, false); fw.flush(); System.out.println("File truncated"); } }OutputFile truncated

Read More

How to capture file not found exception in Java?

karthikeya Boyini
karthikeya Boyini
Updated on 20-Feb-2020 444 Views

While using FileInputStream, FileOutputStream, and RandomAccessFile classes, we need to pass the path of the file to their constructors. In case of a file in the specified path does not exist a FileNotFoundException is raised.Examplepublic class Sample { public static void main(String args[]) throws Exception { File file = new File("myFile"); FileInputStream fis = new FileInputStream(file); System.out.println("Hello"); } }OutputException in thread "main" java.io.FileNotFoundException: myFile (The system cannot find the file specified) ...

Read More

Can we define a class inside a Java interface?

V Jyothi
V Jyothi
Updated on 20-Feb-2020 7K+ Views

Yes, you can define a class inside an interface. In general, if the methods of the interface use this class and if we are not using it anywhere else we will declare a class within an interface.Exampleinterface Library { void issueBook(Book b); void retrieveBook(Book b); public class Book { int bookId; String bookName; int issueDate; int returnDate; } } public class Sample implements Library ...

Read More

What is the Eclipse keyboard shortcut for "public static void main(String[] args) " in Java?

Ramu Prasad
Ramu Prasad
Updated on 20-Feb-2020 9K+ Views

To get public static void main(String[] args) line in eclipse without typing the whole line type main and press Ctrl + space then, you will get the option for the main method select it.

Read More

How to find package explorer in Java eclipse project?

V Jyothi
V Jyothi
Updated on 20-Feb-2020 25K+ Views

To view the project explorer, click on Window menu then, click on Show View and select Project Explorer.There is simpler way to open project explorer, when you are in the editor press alt + shift + w and select project explorer.

Read More
Showing 3451–3460 of 4,498 articles
« Prev 1 344 345 346 347 348 450 Next »
Advertisements