Resize an Array in Java

raja
Updated on 30-Jul-2019 22:30:26

5K+ Views

An array cannot be resized dynamically in Java. One approach is to use java.util.ArrayList(or java.util.Vector) instead of a native array.Another approach is to re-allocate an array with a different size and copy the contents of the old array to the new array.Example:class ResizableArray {    public static void main (String[] args) {          int[] a = {1, 2, 3};          a = (int[])resizeArray(a, 5);          a[3] = 4;          a[4] = 5;          for (int i=0; i 0)                   System.arraycopy(oldArray, 0, newArray, 0, preserveLength);             return newArray;       } } Output:1 2 3 4 5

Create Horizontal Slider in Java

Smita Kapse
Updated on 30-Jul-2019 22:30:26

535 Views

To create Horizontal slider in Java, use the Swing JSlider. Let us first create a frame and a Horizontal slider in it −JFrame frame = new JFrame("Frame with Slider"); JSlider slider = new JSlider(JSlider.HORIZONTAL, 0, 100, 70);Now, we will set the values for the slider. Display the ticks −slider.setMinorTickSpacing(5); slider.setMajorTickSpacing(20); slider.setPaintTicks(true); slider.setPaintLabels(true); Add the slider in the panel: JPanel panel = new JPanel(); panel.add(slider);The following is an example to create horizontal slider −Examplepackage my; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JSlider; import javax.swing.WindowConstants; public class SwingDemo {    public static void main(String[] args) {       JFrame frame = new ... Read More

Resolve Syntax Error Near 'ORDER BY' in MySQL

George John
Updated on 30-Jul-2019 22:30:26

3K+ Views

The word order is a reserved order in MySQL and you have used it in the query. To get rid of the syntax error, you need to use backticks(` `) around the order.The correct syntax is as follows −select *from yourTableName ORDER BY `order` DESC;Let us first create a table −mysql> create table DemoTable    (    `order` int    ); Query OK, 0 rows affected (0.58 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values(89); Query OK, 1 row affected (0.19 sec) mysql> insert into DemoTable values(67); Query OK, 1 row affected (0.13 sec) ... Read More

HTML Meta Content Attribute

George John
Updated on 30-Jul-2019 22:30:26

401 Views

The content attribute of the element is used to set the meta information in an HTML document. This can be the information for the description or the keywords, for name attribute.Following is the syntax:Above, the text is the meta information.Let us now see an example to implement the content attribute of the element:Example Live Demo Tutorials Programming tutorials for free:    Java    C++    C    C# OutputIn the above example, under the element, we have set the description: The description i.e. the meta information is set ... Read More

Tools to Find Bugs and Perform Static Analysis in Python

Sri
Sri
Updated on 30-Jul-2019 22:30:26

2K+ Views

Pychecker and Pylint are the static analysis tools that help to find bugs in python.Pychecker is an opensource tool for static analysis that detects the bugs from source code and warns about the style and complexity of the bug.Pylint is highly configurable and it acts like special programs to control warnings and errors, it is an extensive configuration file Pylint is also an opensource tool for static code analysis it looks for programming errors and is used for coding standard. it checks the length of each programming line. it checks the variable names according to the project style. it can also be used as ... Read More

Rules for Using Switch Statement in Java

Venkata Sai
Updated on 30-Jul-2019 22:30:26

2K+ Views

A switch statement allows a variable to be tested for equality against a list of values. Each value is called a case, and the variable being switched on is checked for each case.Syntaxswitch(expression) {    case value :       // Statements       break;    case value :       // Statements       break;       // You can have any number of case statements.    default :       // Statements }Rules to be followedWhile working with a switch statement keep the following points in mind −We must only use int, ... Read More

HTML DOM Location Replace Method

AmitDiwan
Updated on 30-Jul-2019 22:30:26

230 Views

The HTML DOM Location replace() method is used for rendering a new document replacing the current document. It also removes the current document URL from document history disabling navigation to old document using ‘back’ button.SyntaxFollowing is the syntax −location.replace(URLString)ExampleLet us see an example for Location replace() property − Live Demo Location replace()    form {       width:70%;       margin: 0 auto;    text-align: center;    }    * {       padding: 2px;       margin:5px;    }    input[type="button"] {       border-radius: 10px;    } ... Read More

C qsort vs C++ sort

Smita Kapse
Updated on 30-Jul-2019 22:30:26

605 Views

Here we will see what are the differences between qsort() in C, and sort() in C++.The C provides qsort() function, that can be used for sorting an array. The function arguments and syntax is like below.void qsort(void *base, size_t num, size_t size, int (*comparator) (const void*, const void*));This function takes the base address of that array, the number of elements of that array. Size of each item in the array, and a comparator function.The C++ provides sort() function. This is present inside C++ STL. The arguments and syntax is like below.void sort(T first, T last, Compare c);Here the order of ... Read More

Schedule Notification in Android

Anvi Jain
Updated on 30-Jul-2019 22:30:26

657 Views

This example demonstrate about How to schedule notification in android.Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project.Step 2 − Add the following code to res/layout/activity_main.xml. Step 3 − Add the following code to res/menu/main_menu.xml.             Step 4 − Add the following code to src/MainActivity.package app.tutorialspoint.com.notifyme ; import android.app.AlarmManager ; import android.app.Notification ; import android.app.NotificationManager ; import android.app.PendingIntent ; import android.content.Context ; import android.content.Intent ; import android.os.Bundle ; import android.os.SystemClock ; import android.support.v4.app.NotificationCompat ; import android.support.v7.app.AppCompatActivity ; ... Read More

Retrieve Nested Object in MongoDB

Smita Kapse
Updated on 30-Jul-2019 22:30:26

1K+ Views

To retrieve a nested object in MongoDB, use $ operator. Let us first create a collection with documents −> db.queryNestedObject.insertOne( ...    { ...       "StudentName" : "James", ...       "StudentSubjectScore" : [ ...          {"StudentMongoDBScore":98}, ...          {"StudentCScore":92}, ...          {"StudentJavaScore":91} ...       ] ...    } ... ); {    "acknowledged" : true,    "insertedId" : ObjectId("5ccf49a9dceb9a92e6aa1962") }Following is the query to display all documents from a collection with the help of find() method −> db.queryNestedObject.find().pretty();This will produce the following output −{   ... Read More

Advertisements