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 6 years ago.
Improve this question
I have an array like this:
1101 "TV"
5531 "Baju Baru"
1425 "Mesin Cuci"
Then i want to remove "TV" from my Arraylist. So i must type "1101" then the value is remove. But if i'm wrong it show "code is invalid".
Here is my code:
for (int i = 0; i < listBarang.size(); i++) {
System.out.println(listBarang.get(i));
}
System.out.println("Your code stuff: ");
int code = Integer.parseInt(input.next());
listBarang.remove(i);
Any answer?
Consider using Map to store your arrays and you can remove the TV element from the it. try this.
Map<Integer,String> map = new HashMap<>();
map.put(1101,"TV");
map.put(5531 ,"Baju Baru");
map.put(1425 ,"Mesin Cuci");
for (Map.Entry<Integer,String> hh : map.entrySet()) {
if (hh.getKey() == 1101){
map.remove(hh.getKey());
}
}
System.out.println(map);
output without "TV"
{1425=Mesin Cuci, 5531=Baju Baru}
remove(i) makes no sense outside of your loop. Move it inside and if the code equals the input remove(i).
Related
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 1 year ago.
Improve this question
I am trying to convert the following Java code into swift, but I do not know about this data structure new (int start, int end)[input. length], any guidance would be appraciated.
public override void collection_entries(int[] input)
{
var ranges = new (int start, int end)[input.length];
}
In swift, you should use a Range().
let start = 0
let end = 6
var ranges = Range(start...end)[input.count]
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 8 years ago.
Improve this question
CODE:
for (int i =0; i<=10; i++) {
System.out.println(movieasArrayList.get(i).getID() + " " + movieasArrayList.get(i).getAction());
}
QUESTION:
Change this for-loop to a for-each loop to perform the same task for all Movies (not just first 11).
Any help will be appreciated!!
The for-each loop structure in java goes as follows:
// works with collections and arrays, everything
// that has an iterator
Collection<Object> c = new Collection<Object>();
for (Object i : c) {
// do code
}
For you, this would look like this:
// I don't know what your movieArrayList contains
for (Movie i : movieArrayList) {
System.out.println(i.getId() + " " + i.getAction());
}
Hope this helps!
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 8 years ago.
Improve this question
This is homework. I get the logic but i got stuck on the code. I've done it with normally way and it takes 1 week to get the code. I need to get repeat string with recursive way in Java.
This is my code :
static String repeatString (final int n, final String[] syllables, final String currentWord) {
if (n == 0) {
System.out.println(currentWord);
} else {
for (int i = 0; i < syllables.length; i++) {
repeatString(n - 1, syllables, currentWord + syllables[i]);
}
}
return "";
}
if i call in main method like
String[] str = {"a", "b"};
repeatString(1, str, " ");
then i get output (a,b) if i change to
repeatString(2,str," ");
then i get output ( aa,ab,ba,bb) if i change to
repeatString(3,str," ");
then i get output (aaa,aab,aba,abb,baa,bab,bba,bbb) and so on.
So basically it is like 2 to the power to n. If n=1, i got 2, if n=3, i got 8, and so on.
I would be grateful if someone can help me to get this code in recursive way.
Any help is much appreciated.
The method you have there is recursive already. Being recursive does NOT mean it should have no for loops. A recursive method in cheap words means the method calls itself, which yours does.
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 8 years ago.
Improve this question
i want to store string value in array or in in separate variable using java please short out my problem
Thanks
String a1,a2,a3;
while (resultSet.next()) {
isFound=true;
jLabel2.setText("hello");
String s1=resultSet.getString("Court_Num");
System.out.println(s1);
output s1=: 101
102
103
Desired out put: a1=101
a2=102
a3=103
Your question is confusing I think for 1st record you want a1=101 for 2nd a2=102 etc
int i=1;
while (resultSet.next()) {
if(i==1)
{String s1=resultSet.getString("Court_Num");
System.out.println(a1+"="+s1);}
if(i==2)
{String s1=resultSet.getString("Court_Num");
System.out.println(a2+"="+s1);}
if(i==3)
{String s1=resultSet.getString("Court_Num");
System.out.println(a3+"="+s1);}
i++
The title mentioned using an array. That would look something like this:
String[] a = new String[3];
for(int i=0; resultSet.next(); i++) {
a[i] = resultSet.getString("Court_Num");
System.out.println("a" + i + "=" + a[i]);
}
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 8 years ago.
Improve this question
public static int secret(int value) {
int prod = 1;
for(int i =1; i <= 3; i++) {
prod = prod * value;
}
return prod;
}
What would be the output of:
System.out.println("First secret call: " + secret(5));
System.out.println("Second secret call: " + (2 * secret(6)));
and what does the method secret do
The output would be 125 and then 432. The method, "secret", cubes the number passed into it.
everything fits in a very small screenshot in two windows... try it, you will see it is fun, like playing lego but with more magic!!!