Removing element from arraylist [duplicate] - java

This question already has answers here:
Remove Item from ArrayList
(11 answers)
Closed 6 years ago.
Actually I want to do to set the pointer of arraylist to index which I will give, and my program will check if there's still elements then it will remove all from that index which I have given.
public void removefromindex(int index)
{
for (int j = notes.size(); j >notes.size(); j++)
{
notes.remove(j);
}
}
public void deleteAll(){
for (Iterator<Note> it = notes.iterator(); it.hasNext(); )
{
Note note = it.next();
it.remove();
}
}

You are giving an index and then not even using it and just looping through the ArrayList and removing some elements. What you are looking for is:
public void removefromindex(int index)
{
notes.remove(index);
}
To remove all of them, you can simply use:
public void deleteAll()
{
notes.clear();
}

Related

Using public boolean add(Object new) { [duplicate]

This question already has answers here:
Method for adding Objects into a fixed collection(Array) in Java
(3 answers)
Closed 10 months ago.
In my program I have two private variable
private Object[] array;
private int place;
I have initialized these variables in the constructor
public Arrays() {
array = new Object[10];
place = 0;
}
I am trying to properly implement the following method, this is what I have so far
public boolean add(Object new) {
for(int j = place+1; j <= 0; j++){
array[j] = new;
}
place++;
return true;
}
I am having trouble with placing the object 'new' into the next unoccupied cell
Just assign to the next free slot. new is a reserved keyword.
public boolean add(Object newItem) {
vals[place++]=newItem;
return true;
}

Java nullpointerException when initializing an arraylist [duplicate]

This question already has answers here:
What is a NullPointerException, and how do I fix it?
(12 answers)
Closed 2 years ago.
I tried to build the node object with a arraylist element for adding child node, and initialized successfully. However when i tried to add nodes into the arraylist the exception happened. How to solve the problem?
public class nodes{
int value=-2;
boolean root=false;
nodes parent;
ArrayList<nodes>Children;
public nodes(int value,ArrayList Children) {
this.value=value;
this.Children=Children;
}
public void setParent(nodes parent) {
this.parent=parent;
}
public void Add(nodes child) {
this.Children.add(child);
}
}
public class TreeHeight {
int n;
int parent[];
nodes Nodes[];
void read() throws IOException {
FastScanner in = new FastScanner();
n = in.nextInt();
ArrayList[]Children=new ArrayList[n];
parent = new int[n];//parent is an array that contains all elements
Nodes=new nodes [n];
for (int i = 0; i < n; i++) {
//parent[i]= ;
//index[i]=i;
Nodes[i]=new nodes(in.nextInt(),Children[i]);
}
//Nodes[0].Add(Nodes[1]);
for (int vertex = 0; vertex < n; vertex++) {
if(Nodes[vertex].value==-1) {Nodes[vertex].root=true;
}
for(int j=0;j<n;j++) {
if(Nodes[j].value==vertex) {
Nodes[j].setParent(Nodes[vertex]);
Nodes[vertex].Add(Nodes[j]);
}
}
}
}
I don't see add values for this array?
ArrayList[]Children=new ArrayList[n];
but have get value in this line?
Nodes[i]=new nodes(in.nextInt(),Children[i]);

Why is Java giving my ArrayList an IndexOutOfBoundsException? [duplicate]

This question already has answers here:
What causes a java.lang.ArrayIndexOutOfBoundsException and how do I prevent it?
(26 answers)
Closed 2 years ago.
Why is Java giving me an error message?
Im using JavaSE-12 and don't understand why I have an IndexOutOfBoundsException.
Do I have to specify the index of the ArrayList or what am I missing?
import java.util.ArrayList;
public class DSArrayList {
public static void main(String[] args) {
// TODO Auto-generated method stub
ArrayList<String> namensListe = new ArrayList<String>();
namensListe.add("Hans");
namensListe.add("Peter");
for(int i = 0; i <= namensListe.size(); i++) {
System.out.println("Name: " + namensListe.get(i));
}
}
}
for(int i = 0; i < namensListe.size(); i++)
(note the < operator)
To explain, arrays and like structures are indexed from zero, not one. Obviously we count, however, from one, not zero. So the length of an array is always one greater than the index of the last item it contains.

Return element's index in ArrayList<Object> that contains some field [duplicate]

This question already has answers here:
Stream Way to get index of first element matching boolean
(8 answers)
Closed 2 years ago.
I have an ArrayList of say, this object:
class Object() {
String name;
double price;
int id;
}
I want to find the index of the ArrayList that contains the element where the name field is equal to a given string. For example:
public int findIndex(ArrayList<Object> list, String name) {
for(Object o : list) {
if (o.getName().equals(name) {
//return index
}
}
}
What's a good way to return the index?
You can iterate the list with an indexed-for-loop:
for(int i = 0; i < list.size(); i++) {
if (list.get(i).getName().equals(name)) {
return i;
}
}
return -1;
Or simply use a counting variable when you want to use the for-each-loop:
int i = 0;
for(Object o : list) {
if (o.getName().equals(name)) {
return i;
}
i++;
}
return -1;
Or using Java-8 streams:
return IntStream.range(0, list.size())
.filter(i -> list.get(i).getName().equals(name))
.findFirst()
.orElse(-1);
You see that I returned -1 when nothing was found. This is a common concept used throughout the java language.
I think you can just do:
public int findIndex(ArrayList<Object> list, String name) {
for(Object o : list) {
if (o.getName().equals(name) {
return list.indexOf(o);
}
}
return -1;
}
(-1 is returned if nothing was found).

Filling an array using a method [duplicate]

This question already has answers here:
What's the simplest way to print a Java array?
(37 answers)
Closed 7 years ago.
I'm stuck with a revision question from a lab. The question is:
Write a static method to initialize an array of integers called numList. The size of the array should be passed as an int to the method and the array should be returned. Each odd index position should contain the value -1 and each even numbered position should contain the index value. Thus, such an array might contain {0,-1,2,-1,4,-1,6,-1}.
My code is currently :
public class initializeArray{
public static void main (String [] args) {
int [] numList = new int [6];
alterArray(numList);
}
public static void alterArray (int [] numList)
{
for( int i = 0; i<numList.length; i++)
{
if (i == 0)
{
numList[i] = i;
} else{
numList[i] = -1;
}
}
System.out.println( "The array is: " + numList);}
}
The return that I'm getting is :
"The array is: [I#1ef856c"
Thanks.
Updated to:
System.out.println(Arrays.toString(numList));
Error now occuring:
"Cannot find symbol - variable Arrays"
Try this:
System.out.println(Arrays.toString(numList));

Categories

Resources