What does it mean by Object [ ] [closed] - java

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I am sorry if this question is silly question, but I have hard time to understand the usage of Object []. I want to know what does below method really return. because it is enveloped as an object. Why i m asking this because i want to replace this method with other method to integrate with other module.
What I understand it would return list of Value Object? But what is this Object[] really means?
Thank you soo much.
public Object[] generateNewGreedySolution(int startingPoint) {
if (startingPoint == -1) {
startingPoint = new Random().nextInt(ins.getDimension() + 1);
}
//Solution s = new Solution(problem);
Object[] values = new Object[ins.getDimension()];
List<Integer> cities = new ArrayList<Integer>();
for (int i = 0; i < ins.getDimension(); i++) {
if ((i + 1) != startingPoint) {
cities.add(i + 1);
}
}
values[0] = startingPoint;
for (int i = 1; i < values.length; i++) {
double minCost = -1;
int index = -1;
for (int j = 0; j < cities.size(); j++) {
double distance = ins.getDistance(startingPoint, cities.get(j));
if (distance < minCost || minCost == -1) {
minCost = distance;
index = j;
}
}
values[i] = cities.get(index);
startingPoint = cities.get(index);
cities.remove(index);
}
return values;
}

Class Object is the root of the class hierarchy. Every class has Object as a superclass. All objects, including arrays, implement the methods of this class.
from: http://docs.oracle.com/javase/7/docs/api/java/lang/Object.html
Meaning that you are able to use an array of the type Object to return a collection of just about every class object. For example you could have the object with index 0 be an Integer another one being a String.
But since "only" all the classes are inherited, you need to notice that primitives are NOT. What I mean with that is that there is for example a difference between int (primitive) and an java.lang.Integer (which is inherited from said Object and has an int member variable, but can also point to null).

your generateNewGreedySolution method returns an array of Object Type
In the 5th line you are doing
Object[] values = new Object[ins.getDimension()];
So here values is an object array and hence you are returing the same.
If you are confused of what to return in methods then please note that if your method signaure is int then you should return int,if your method signature is String then you should return String.
Here your method return type is Object[] and so you are returning values

Related

readNumberAsArray assignment [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 3 years ago.
Improve this question
Write a readNumberAsArray method that takes an integer as a parameter and creates a new int array with that number as the length. Subsequently, a corresponding number of int values ​​should be read in with the aid of the IOTools and the array filled with them returned, whereby only single-digit numbers (0-9) should be taken into account as input. If the parameter is negative, the method should return null. For negative or two-digit value entries, the entered value should be replaced by 0. Use a for loop to read in the values. A text output when using the IOTools is not necessary.
My program is not working.
import Prog1Tools.IOTools;
package com.company;
public class Main {
public static void readNumberAsArray(int a) {
int [] a = new int[];
int a = IO.Tools.readInteger () ;
for int (a = 0 ; a<10 ; --a) {
System.out.println('0');
for (int a=0; a>10; a++) {
System.out.println(a);
for (int a=10; a=>10; a++) {
System.out.println('0');
}
}
}
// write your code here
}
}
I think you have to just implement what you're asked. Step by step. There's no special logic.
public int[] readNumberAsArray(int n) {
// negative or two-digit values should be replaced with 0
if (n <= 0 || n > 9)
return new int[0];
// creates a new int array with that number as the length
int[] arr = new int[n];
// corresponding number of int value should be read in with the aid of the IOTools
for (int i = 0; i < arr.length; i++)
arr[i] = IO.Tools.readInteger();
return arr;
}

I want to hold the value of a variable in java outside the loop [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I am having a global variable and I want to concatenate some value to this variable in a for loop and want the value outside of a for loop.
But the problem is whenever the for loop starts it's next iteration value of variable is lost.
my code
function hello() {
StringBuffer Id = new StringBuffer(20);
Id.append("");
for (i = 1; i < 10; i++) {
Id.append(i);
}
System.out.println(Id);
}
You need System.out.println(id); based on your comment that has your code.
You just need to return id from your method to caller to hold the value.
String[][] matrix = { {"1", "2", "3"} };
String[] y = {"TEST" ,"BUG"};
int a = 0;
int value = 0;
for (int i = 0; i < y; i++)
{
for (int j = 1; j < 4; j++)
{
value = Integer.parseInt(matrix[i][j - 1]);
System.out.println(value); //this is OK it print me 3 values
}
}
System.out.println(value);
Declaring variables inside or outside of a loop

How to use a variable outside of a for loop [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I'm trying to convert multiple strings into integers and then print use each one of them outside of the for loop.
This is my for loop:
for (int i = 0; i < parts.length; i++) {
int p1 = Integer.parseInt(parts[i]);
}
If you want to be able to access all of the integers you are parsing after the loop:
int[] pValues = new int[parts.length];
for (int i = 0; i < parts.length; i++) {
pValues[i] = Integer.parseInt(parts[i]);
}
// at this point you still have access to the pValues array
In order to use anything outside a for loop (or any other scope, for that matter) you must declare that variable in the scope where you wish to use it:
int p1 = -1;
for (int i = 0; i < parts.length; i++) {
p1 = Integer.parseInt(parts[i]);
}
Note that in situations like the above, where a loop sets a single value, a very common thing is to break the loop right after setting the value. Obviously, the value should be set conditionally, otherwise the purpose of the loop would be defeated:
int p1 = -1;
for (int i = 0; i < parts.length; i++) {
if (someCondition()) {
p1 = Integer.parseInt(parts[i]);
break;
}
}
Any variables created inside of a loop are LOCAL TO THE LOOP. This means that once you exit the loop, the variable can no longer be accessed! This includes any variables created in the loop signature.
Read more: http://www.java-made-easy.com/variable-scope.html#ixzz3TZ6obLXK
int secondp1 = 0; // Define your variable outside your loop
for (int i = 0; i < parts.length; i++) {
secondp1 = Integer.parseInt(parts[i]);
}
//so you can use it here.
System.out.println(secondp1 );
You can see this to check out variable scope and lifetime.

Determine whether or not an array of integers has unique items [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
I'm working on a method that will compare values in an array of random integers. The method is supposed to check for uniqueness. The array would be considered unique if there are no duplicate integers, otherwise it is not unique. The curveball is I have to return a boolean value. It compiles but then it ends up crashing. Any suggestions?
public static boolean isUnique(int[] list) {
boolean unique = true;
do {
for (list i = 0; i < array.length; i++) { // cycle through array
if (list[i] != list[i + 1]) // checks to see if first element is not equal to 2nd
{
unique = true;
}
}
} while (unique);
{
if (unique == true) {
System.out.println("This list contains all unique values");
}
return true; // if all values are unique return true
}
else
System.out.println("This list contains duplicates");
return false; // otherwise the array contains dublicate
}
Two problems here--
First the crash-- you are checking against the next value in the list; however, when you are at the end of the list (i = array.length - 1) the next value will be off the end of the list.
Second, you probably can't assume the values are sorted beforehand, so you don't know that checking against the adjacent value will find all duplicates.
Instead if all that code, you could let the JDK do the heavy lifting for you:
Set<Integer> set = new HashSet<Integer>();
for (int i : list)
set.add(i);
return set.size() == list.length;
If your list variable was actually a List, you could do it in one line:
public static boolean isUnique(List<Integer> list) {
return new HashSet<Integer>(list).size() == list.size();
}
If you wanted to "overachieve", you could make this last version both generic, so it would work with any type, and work for any collection as well:
public static boolean isUnique(Collection<?> list) {
return new HashSet<Object>(list).size() == list.size();
}
Note that a type of Object for the internal Set does not invalidate the correctness of behaviour - the set doesn't care what the type is, only that equals() and hashCode() for the type is correctly implemented.
Everything in your method is actually wrong, you are entering infinite loop, have useless constructions
here is one of possible variant to check array for uniqueness:
public static boolean isUnique(final int[] list) {
for (int i = 0; i < (list.length - 1); i++)
for (int j = (i + 1); j < list.length; j++)
if (list[i] == list[j])
return false;
return true;
}

What's wrong with this code that it doesn't compile? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Closed 9 years ago.
Improve this question
For some reason the true and false are considered "incompatible types". Am I only suppose to run this through a boolean method? What's wrong with it.
for(int i = 0; i < array.length ; i++)
{
int val = (array[i] % 2);
if(val == 0)
array[i] = true;
else
array[i] = false;
}
Well array is probably an int[], given that you're using array[i] % 2 and assigning the result to an int.
There's no conversion from boolean to int, so you can't store your result back in the int[] array. It's not clear what you're trying to do, but that's why it's not compiling.
Aside
If you had a separate boolean[] of the same size, that would work - although it would be more simply written as:
boolean[] even = new boolean[array.length];
for (int i = 0; i < array.length; i++) {
even[i] = (array[i] % 2) == 0;
}
Any time you find yourself with:
if (someCondition) {
doSomething(true);
} else {
doSomething(false);
}
you should consider refactoring it to:
doSomething(someCondition);
Your array contains wrong types:
int[] a = {1, 2, 4};
boolean[] b = {true, false};
b[0] = 1; //error
a[0] = 1; //ok

Categories

Resources