“Arrays” Code-Antworten

JavaScript -Array

//create an array like so:
var colors = ["red","blue","green"];

//you can loop through an array like this:
for (var i = 0; i < colors.length; i++) {
    console.log(colors[i]);
}
Grepper

Arrays

int[] numbers = new int[10];

int[] names = new String[]{"John", "Jack"};

int length = names.length;

for (String name: names) {
    System.out.println(name);
}
Hilarious Hippopotamus

Arrays

itemp:integer
Confused Cat

Arrays

// splice() returns NEW array !!!
let array = ['today', 'was', 'not', 'so', 'great'];
array.splice(2, 2); //['today', 'was', 'great']

//*************************************************************************'*/

let array = ['I', 'am', 'feeling', 'really', 'happy'];
let newArray = array.splice(3, 2); // newArray has the value ['really', 'happy']

//*************************************************************************'*/

// Add items with splice()
const numbers = [10, 11, 12, 12, 15];
const startIndex = 3;
const amountToDelete = 1;

numbers.splice(startIndex, amountToDelete, 13, 14);
console.log(numbers);

// The second entry of 12 is removed, and we add 13 and 14 at the same index.
// The numbers array would now be [ 10, 11, 12, 13, 14, 15 ]

//*************************************************************************'*/
// Copying array items with splice()

let weatherConditions = ['rain', 'snow', 'sleet', 'hail', 'clear'];
let todaysWeather = weatherConditions.slice(1, 3);

//todaysWeather would have the value ['snow', 'sleet'], while weatherConditions
//would still have ['rain', 'snow', 'sleet', 'hail', 'clear'].
//In effect, we have created a new array by extracting elements from an existing array.

//*************************************************************************'*/
// slice() return NEW array   (NOTICE slice()!!! )

let weatherConditions = ['rain', 'snow', 'sleet', 'hail', 'clear'];
let todaysWeather = weatherConditions.slice(1, 3);

//todaysWeather would have the value ['snow', 'sleet'], while weatherConditions
//would still have ['rain', 'snow', 'sleet', 'hail', 'clear'].

//*************************************************************************'*/
// Combine arrays with spread

let thisArray = ['sage', 'rosemary', 'parsley', 'thyme'];
let thatArray = ['basil', 'cilantro', ...thisArray, 'coriander'];

// thatArray would have the value ['basil', 'cilantro', 'sage', 'rosemary', 'parsley', 'thyme', 'coriander']

//*************************************************************************'*/
// indexOf()

let fruits = ['apples', 'pears', 'oranges', 'peaches', 'pears'];

fruits.indexOf('dates');
fruits.indexOf('oranges');
fruits.indexOf('pears');

// indexOf('dates') returns -1, indexOf('oranges') returns 2, and
// indexOf('pears') returns 1 (the first index at which each element exists).

//*************************************************************************'*/

function filteredArray(arr, elem) {
  let newArr = [];

  for (let i = 0; i < arr.length; i++) {
    // if elem is NOT in the array... push arr to the newArr
    if (arr[i].indexOf(elem) === -1) newArr.push(arr[i]);
  }
  return newArr;
}

console.log(
  filteredArray(
    [
      [3, 2, 3],
      [1, 6, 3],
      [3, 13, 26],
      [19, 3, 9],
    ],
    3 // <------------------------
  )
);
YosKa

Arrays

ia = np.array([[0, 0], [2, 2]])
ja = np.array([[0, 0], [3, 3]])
ka = np.array([[0, 3], [0, 3]])
Healthy Hedgehog

Arrays

import java.io.*; 
import java.util.*; 

class GFG { 
	public static void main(String[] args) 
	{ 
		// Let us create different types of arrays and 
		// print their contents using Arrays.toString() 
		boolean[] boolArr = new boolean[] { true, true, false, true }; 
		byte[] byteArr = new byte[] { 10, 20, 30 }; 
		char[] charArr = new char[] { 'g', 'e', 'e', 'k', 's' }; 
		double[] dblArr = new double[] { 1, 2, 3, 4 }; 
		float[] floatArr = new float[] { 1, 2, 3, 4 }; 
		int[] intArr = new int[] { 1, 2, 3, 4 }; 
		long[] lomgArr = new long[] { 1, 2, 3, 4 }; 
		Object[] objArr = new Object[] { 1, 2, 3, 4 }; 
		short[] shortArr = new short[] { 1, 2, 3, 4 }; 

		System.out.println(Arrays.toString(boolArr)); 
		System.out.println(Arrays.toString(byteArr)); 
		System.out.println(Arrays.toString(charArr)); 
		System.out.println(Arrays.toString(dblArr)); 
		System.out.println(Arrays.toString(floatArr)); 
		System.out.println(Arrays.toString(intArr)); 
		System.out.println(Arrays.toString(lomgArr)); 
		System.out.println(Arrays.toString(objArr)); 
		System.out.println(Arrays.toString(shortArr)); 
	} 
} 
Hervi May

Ähnliche Antworten wie “Arrays”

Fragen ähnlich wie “Arrays”

Durchsuchen Sie beliebte Code-Antworten nach Sprache

Durchsuchen Sie andere Codesprachen