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

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;
}

Related

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]);

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).

java poker game nullpointer [duplicate]

This question already has answers here:
What is a NullPointerException, and how do I fix it?
(12 answers)
Closed 6 years ago.
I'm getting a null pointer exception for this and I'm not sure why
public static boolean hasPair(Card[] cards) {
int k=0;
cards = new Card[5];
for (int atPos = 0; atPos<5; atPos++){
for (int atPos2 = atPos+1; atPos2<5; atPos2++){
if(cards[atPos].getValue() == cards[atPos2].getValue()){
k++;
}
if (atPos2 == (cards.length-1) && k!=1){
k=0;
}
else if (atPos2 == (cards.length-1) && k>=2){
return true;
}
}
}
return false;
}
My method is testing whether or not my hand of cards has two cards that hold the same value and the nul pointer says it's within this line
if(cards[atPos].getValue() == cards[atPos2].getValue()){
I also have this method...could i use it as a helper?
public Card[] deal(int numCards) {
Card[] newArray;
newArray = new Card[numCards];
for (int index=0; index<numCards; index++){
newArray[index] = cards.get(0);
cards.remove(0);
}
return newArray;
}
In second line you create a new array of objects Card. Every object in that array is null, so you need to fill the array first.

Removing element from arraylist [duplicate]

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();
}

Checking for duplicates in an int array [duplicate]

This question already has answers here:
Java Array, Finding Duplicates
(17 answers)
Closed 9 years ago.
Would this be the correct method to check whether an integer array contains duplicates? I wanted to pass in an int[] nums instead of Integer[], but couldn't get that to work.
public static boolean isUnique(Integer[] nums){
return new HashSet<Integer>(Arrays.asList(nums)).size() == nums.length;
}
You can do something like:
public static boolean isUnique(int[] nums){
Set<Integer> set = new HashSet<>(nums.length);
for (int a : nums) {
if (!set.add(a))
return false;
}
return true;
}
This is more of a short-circuit-esque approach than what you have, returning as soon as it encounters a duplicate. Not to mention it works with an int[] as you wanted. We are exploiting the fact that Set#add returns a boolean indicating whether the element being added is already present in the set.
Whether Set or sorting is irrelevant here, and sorting is more optimal, less objects.
public static boolean isUnique(int[] nums) {
if (nums.length <= 1) {
return true;
}
int[] copy = Arrays.copyOf(nums);
Arrays.sort(copy);
int old = Integer.MAX_VALUE; // With at least 2 elems okay.
for (int num : copy) {
if (num == old) {
return false;
}
old = num;
}
return true;
}
Addendum As commented slower, though saving memory.

Categories

Resources