Found 33676 Articles for Programming

New Array from Index Range Swift

Nitin Aggarwal
Updated on 11-Apr-2023 10:53:41

1K+ Views

In Swift, you can create an array using the ArraySlice type. The following examples will show how to use the ArraySlice type. You can create an extension as well to define the subscript method. Example 1 In the following example, we create an array of numbers with values from 0 to 9 and then create a range of indices from startIndex to endIndex (exclusive). Using this range we can generate an ArraySlice of the original array. In the end, we will convert the ArraySlice to an array using the Array initializer and print the result. import Foundation let numbers = ... Read More

Multiple Type Constraints in Swift

Nitin Aggarwal
Updated on 11-Apr-2023 10:50:39

1K+ Views

In Swift, there are several ways to implement type constraints on types. We will use some common approaches like the where clause, protocol, etc. Generics provide lots of flexibility to write better and more secure code in Swift. We can apply generics to collections, custom types, etc. One of them is generic type constraints. Using type constraints, you can make your generic code behave that matches a certain set of constraints whatever you define. Swift provides multiple ways to specify type constraints on generic type parameters. Type constraints using the "where" clause The "where" clause in Swift is a highly ... Read More

Java Program to Concatenate Two List

Bharti Kumari
Updated on 31-May-2024 16:15:26

9K+ Views

Introduction In Java, a list is an ordered collection of elements that allows duplicates. Sometimes, we may need to combine two lists into one list. Concatenating two lists means joining the elements of the two lists to form a new list containing all the elements of both lists. There are various ways to concatenate two lists in Java, including using loops, streams, and built-in methods. In this context, we will explore different approaches to concatenate two lists in Java. One approach is to use the addAll() method provided by the List interface. This method adds all the elements of one ... Read More

Java program to compute the sum of numbers in a list using while-loop

Bharti Kumari
Updated on 21-Oct-2024 17:42:02

680 Views

In this article, we will learn to compute the sum of numbers in a list using Java with a while-loop. We will look at two examples: one that sums a list of integers and another that sums a list of double values. For each example, we will create an ArrayList to store the numbers, and then use a while-loop to iterate through the list. During the iteration, we will add each number to a variable called "sum, " which will keep track of the total. This will help you understand how to use loops and ArrayLists in Java effectively. Different ... Read More

Java Program to Compute the Sum of Numbers in a List Using For-Loop

Bharti Kumari
Updated on 10-Apr-2023 15:58:44

3K+ Views

Introduction Java is a popular programming language that is used for developing a wide range of applications, including those that involve working with lists of numbers. One common task is to compute the sum of numbers in a list, which can be accomplished using a for-loop. In this approach, we first create a list of numbers, then initialize a variable to hold the sum. We then use a for-loop to iterate over each element in the list, adding it to the sum variable. After the loop has completed, the sum variable contains the total sum of all the numbers in ... Read More

Java Program to Compute the Running Total of a List

Shriansh Kumar
Updated on 30-Jul-2024 16:44:30

1K+ Views

A running total (also known as an accumulated total) of a list represents the sum of a sequence of elements. As new elements are added to the sequence, the running total continuously updates. To perform this operation in Java, we can use for loop and while loop in our program. The program will have a time complexity of O(n), where n is the number of elements in the list because it loops through the list once and performs a constant-time operation for each element in the list. List is an interface of Java Collection Framework that stores collections of objects. ... Read More

Java Program to Compute the Area of a Triangle Using Determinants

Bharti Kumari
Updated on 10-Apr-2023 15:56:57

391 Views

Introduction The Java program to compute the area of a triangle using determinants is a concise and efficient program that calculates the area of a triangle given the coordinates of its three vertices. This program is useful for anyone studying or working with geometry, as it demonstrates how to use basic arithmetic and algebraic calculations in Java, and how to read in user input using the Scanner class. The program prompts the user to enter the coordinates of three points of the triangle, which are then read in and used to calculate the determinant of the matrix of coordinates. The ... Read More

Java Program to Compare Two Objects

Shriansh Kumar
Updated on 31-Jul-2024 19:55:42

1K+ Views

To compare objects in Java, check the properties or the hash value of given objects. If the properties of both objects matches, then they are equal. If either property is different, it indicate that the objects are not equal. NOTE: Don't use equality operator (==) to compare objects as it checks only their memory location. Objects comparison should be done based on the properties not location. How to Compare Objects in Java? To compare two objects in Java, use the following approaches − By overriding equals() method Without overriding equals() method ... Read More

Java program to combine two list by alternatively taking elements

Bharti Kumari
Updated on 21-Aug-2024 00:06:57

1K+ Views

Introduction The Java program to combine two lists by alternatively taking elements is a simple code snippet that takes in two lists of any type of object that can be stored in a list and returns a new list that contains elements from both input lists alternately. The program defines a method called alternateMerge that takes two lists as input and returns a new list that contains elements from both lists alternately. It determines the size of both lists and loops through the larger of the two lists. For each index in the loop, it adds the element from the ... Read More

Java Program to Check the Accessibility of an Static Variable By a Static Method

Bharti Kumari
Updated on 10-Apr-2023 15:50:34

311 Views

Introduction In Java, we can define variables and methods as static. A static variable or method belongs to the class itself rather than to the individual objects of the class. Therefore, we can access a static variable or method using the class name, without creating an object of the class. In this program, we will explore how to check the accessibility of a static variable by a static method. We will define a class with a static variable and a static method that accesses the variable. We will then call the static method to check if it can access the ... Read More

Advertisements